Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "if all" in python

Why this work

for td in alltd:
    if "style3" in td["class"] or "style4" in td["class"] or "style1" in td["class"]:
        td["class"] = "s1"

and this not?

for td in alltd:
    if all(x in td["class"] for x in ("style3", "style4", "style1")):
        td["class"] = "s1"
like image 716
a1204773 Avatar asked Dec 18 '25 07:12

a1204773


1 Answers

all([x1,x2,...]) is basically the same as x1 and x2 and ..., not x1 or x2 or ...

>>> all([True, True])
True
>>> all([True, False])
False

Use any() instead.

>>> any([True,False])
True
like image 71
Matthew Adams Avatar answered Dec 19 '25 23:12

Matthew Adams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!