[Tutor] list comprehension, testing for multiple conditions
Pete O'Connell
pedrooconnell at gmail.com
Wed Aug 22 23:19:28 CEST 2012
Thanks eryksun, that is a very clear example.
Cheers
pete
On Wed, Aug 22, 2012 at 11:16 PM, eryksun <eryksun at gmail.com> wrote:
> On Wed, Aug 22, 2012 at 3:06 AM, Peter Otten <__peter__ at web.de> wrote:
>>
>> wanted = [line.strip("\n") for line in lines
>> if "vn" not in line and "vt" not in line and line != "\n"]
>
> Here's an equivalent expression with the negation factored out:
>
> not ("vn" in line or "vt" in line or line == "\n")
>
> http://en.wikipedia.org/wiki/De_Morgan%27s_laws
>
> If you have a lot of tests all using the same operator (e.g. "in"),
> you can use "any" (OR) or "all" (AND) with a generator expression:
>
> vals = ["vn", "vt", "vu", "vv", "vw", "vx", "vy", "vz"]
> wanted = [line for line in lines if not any(v in line for v in vals)]
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
--
-
More information about the Tutor
mailing list