[Tutor] If/elif/else when a list is empty
Vince Spicer
vince at vinces.ca
Tue Sep 14 06:08:43 CEST 2010
On Mon, Sep 13, 2010 at 9:58 PM, <aeneas24 at priest.com> wrote:
> Hi,
>
> I'm parsing IMDB movie reviews (each movie is in its own text file). In my
> script, I'm trying to extract genre information. Movies have up to three
> categories of genres--but not all have a "genre" tag and that fact is making
> my script abort whenever it encounters a movie text file that doesn't have a
> "genre" tag.
>
> I thought the following should solve it, but it doesn't. The basic question
> is how I say "if genre information doesn't at all, just make
> rg1=rg2=rg3="NA"?
>
> rgenre = re.split(r';', rf.info["genre"]) # When movies have genre
> information they store it as <genre>Drama;Western;Thriller</genre>
>
> if len(rgenre)>0:
> if len(rgenre)>2:
> rg1=rgenre[0]
> rg2=rgenre[1]
> rg3=rgenre[2]
> elif len(rgenre)==2:
> rg1=rgenre[0]
> rg2=rgenre[1]
> rg3="NA"
> elif len(rgenre)==1:
> rg1=rgenre[0]
> rg2="NA"
> rg3="NA"
> else len(rgenre)<1: # I was hoping this would take care of the "there is
> no genre information" scenario but it doesn't
> rg1=rg2=rg3="NA"
>
> This probably does a weird nesting thing, but even simpler version I have
> tried don't work.
>
> Thanks very much for any help!
>
> Tyler
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
Hey Tyler you can simplify this with a onliner.
rg1, rg2, rg3 = rgenre + ["NA"]*(3-len(rgenre[:3]))
Hope that helps, if you have any questions feel free to ask.
Vince
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100913/3ca15bbb/attachment.html>
More information about the Tutor
mailing list