使用;“如果全部”;用python
为什么这样做使用;“如果全部”;用python,python,beautifulsoup,Python,Beautifulsoup,为什么这样做 for td in alltd: if "style3" in td["class"] or "style4" in td["class"] or "style1" in td["class"]: td["class"] = "s1" 这不是吗 for td in alltd: if all(x in td["class"] for x in ("style3", "style4", "style1")): td["class"]
for td in alltd:
if "style3" in td["class"] or "style4" in td["class"] or "style1" in td["class"]:
td["class"] = "s1"
这不是吗
for td in alltd:
if all(x in td["class"] for x in ("style3", "style4", "style1")):
td["class"] = "s1"
如果您正在执行或基于兼容的操作,请使用:
`if any(x in td["class"] for x in ("style3", "style4", "style1")):`
有关任何(iterable)的帮助:
如果iterable的任何元素为True,则返回Truei、 e或
条件
有关所有(iterable)的帮助:
如果iterable的所有元素都为True,则返回Truei、 e和
续
all([x1,x2,…])
基本上与x1和x2以及…
相同,而不是x1或x2或…
>>> all([True, True])
True
>>> all([True, False])
False
改用any()
>>> any([True,False])
True
因为使用all()
就像在所有语句之间放置和
。使用any()
复制上述if-statement的行为。要使用all
您必须对any
表达式进行双重否定,并在量词上分配一个否定:notall(x not in td[“class”]表示x in…
)。在这种情况下,这将是愚蠢的,但如果您在测试中看到逻辑否定(例如,!=
),那么切换表单可能会更简单。