[Tutor] list comprehension, testing for multiple conditions

Dave Angel d at davea.name
Fri Aug 24 03:39:29 CEST 2012


On 08/23/2012 09:11 PM, Pete O'Connell wrote:
> Hi, I have tried to simplify things and am running into a bit of trouble.
> What i am really trying to do is: Keep all the lines starting with "v " and
> then delete those lines whose modulus 5 don't equal zero
>
> I have written it like this which seems to take a really long time (a
> couple of  minutes when iteration over a folder with 200 files to parse)
> #####################################
> with open(theFilePath) as lines:
>     #keep only the lines beginning with "v " (this works)
>     theGoodLines = [line.strip("\n") for line in lines if "v " ==
> line[0:2]]

Better to use startswith(), since short lines will cause the if
expression above to blow up.
>     theLinesAsListSubset = []
>     for i in range(len(theGoodLines)):

When you see a line like this, it's usually clearer to do:
         for i, line in enumerate(theGoodLines):
>         nuke.tprint(i)
>         if i%5 != 0:
>             continue
>         elif i%5 == 0:
>             theLinesAsListSubset.append(theGoodLines[i])
It's confusing to me whether you meant to keep only one of every 5 lines
of the filtered input, or to keep only those lines of the filtered input
that came from the appropriate indices of the original data.  You need a
more precise spec before you can safely combine the two loops.  (You may
have it precise in your head;  I'm just saying it isn't clear to me)
 
> ########################################
>
> I think it would be better to include the modulud test within the original
> list comprehension but I am not sure how to access the index of "line":
>     #something like this is a sketch of what I mean (I know it's wrong)
>     theGoodLines = [line.strip("\n") for line in lines if "v " ==
> line[0:2] and line.getIndex() % 5 == 0]
>
>
> Do I need enumerate for this maybe?
Good call.   Question is whether to do the enumerate on the original
list, or on the list you get after.  That decision would be based on the
question above.

To be honest, when I'm doing a non-trivial list comprehension, i tend to
write it as a for loop first, get it correct, then reconsider if it can
(& if it should be) rewritten as a comprehension.



-- 

DaveA



More information about the Tutor mailing list