[Tutor] list comprehension, testing for multiple conditions
eryksun
eryksun at gmail.com
Wed Aug 22 13:16:38 CEST 2012
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)]
More information about the Tutor
mailing list