[Tutor] list comprehension, testing for multiple conditions

Asokan Pichai pasokan at talentsprint.com
Thu Aug 23 12:09:51 CEST 2012


On Thu, Aug 23, 2012 at 2:57 AM, Pete O'Connell <pedrooconnell at gmail.com> wrote:
> On Thu, Aug 23, 2012 at 4:16 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
>> theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListStripped
>>                                                 if "vn" not in x
>>                                                 if "vt" not in x
>>                                                 if x!= ""]
>>
>> It's slightly more verbose but it makes the rules more explicit, IMHO.
>
> I agree, it seems easier to read when written on multiple lines. I'll
> do it that way,
> Thanks
> Pete

You can look at the option of writing a function to do the selection part.
If the function is named well and also corresponds to some domain
operation or concept it can be very useful.

def select(p):
      if p == "":
         return False
      return "vt" In p or "vn" in p
..........

selectedLines = [ x for x in TextAsListStripped if select(x) ]
......

Asokan Pichai

If a language is designed for non-programmers, soon only
non-programs get written in it.                 --- Anonymouse


More information about the Tutor mailing list