Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
149 views
in Technique[技术] by (71.8m points)

python - Why does this not work as an array membership test?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is a chained comparison. You may have known that you can do

1 < 2 < 3

in Python, and it's equivalent to (1 < 2) and (2 < 3). (Or maybe you didn't. Now you know.) Well, the same thing applies to in and ==.

5 in [1, 2, 3, 4] == False

is equivalent to

(5 in [1, 2, 3, 4]) and ([1, 2, 3, 4] == False)

Since [1, 2, 3, 4] is not equal to False, the whole expression evaluates to False.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...