Testing multiple string 'in' conditions in list comprehension [duplicate]

心已入冬 提交于 2019-12-10 12:33:24

问题


I am trying to add multiple 'or' clauses to a python if statement using list comprehension. My code is shown below. I would like to keep the list comprehension. In terms of pseudocode, the logic would simply be:

Alive_Beatles = each name that contains '(Beatle)' and either ('Paul', 'Yoko' or 'Ringo')

The code only returns Paul and skips Ringo and Yoko.

Names = ["John Lennon (Beatle)",  "Paul McCartney (Beatle)", "Ringo Starr (Beatle)", "Yoko Ono (Beatle)", "Mick Jagger (Rolling Stone)", "Brian Jones (Rolling Stone)", "Alex Jones (na)", "Adam Smith (na)"]
Alive_Beatles = [n for n in Names if ("Beatle" and ("Paul" or "Ringo" or "Yoko")) in n]

print Alive_Beatles

回答1:


You need to test each name explicitly if it's in n:

[n for n in Names if ("Beatle" in n and ("Paul" in n or "Ringo" in n or "Yoko" in n))]

Otherwise the and and or use the truth value of you search strings (and each non-empty string is always True) and finally tests if Paul in n (the first truth value of the ors).

The documentation explicitly mentions this:

4.2. Boolean Operations — and, or, not

These are the Boolean operations, ordered by ascending priority:

Operation     Result                                Notes
x or y        if x is false, then y, else x         (1)
x and y       if x is false, then x, else y         (2)
not x         if x is false, then True, else False  (3)

Notes:

(1) This is a short-circuit operator, so it only evaluates the second argument if the first one is false.

(2) This is a short-circuit operator, so it only evaluates the second argument if the first one is true.

(3) not has a lower priority than non-Boolean operators, so not a == b is interpreted as not (a == b), and a == not b is a syntax error.

So "Beatle" and (...) evaluates according to (2) to the second argument because "Beatle" is truthy and according to (1) it evaluates to the first argument of the chained ors: "Paul" because it's also truthy.




回答2:


This doesn't do what you expect because the expression

("Paul" or "Ringo" or "Yoko")

evaluates to "Paul". Type it in at an interpreter prompt to confirm this.

And even that only seems to work because

("Beatle" and ("Paul" or "Ringo" or "Yoko")) 

also evaluates to "Paul".




回答3:


The most straightforward solution is to just list

[n for n in Names if "Beatle" in n and ("Paul" in n or "Ringo" in n or "Yoko" in n)]

But you could use any to get a bit closer to what you tried initially:

[n for n in Names if "Beatle" in n and any(x in n for x in ("Paul", "Ringo", "Yoko"))]


来源:https://stackoverflow.com/questions/48021589/testing-multiple-string-in-conditions-in-list-comprehension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!