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

Categories

I have observed the following behavior in python 3:

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

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

I was expecting exactly the opposite:

[False, True] and [True, True] = [False and True, True and True] = [False, True]
[False, True] or [True, True] = [False or True, True or True] = [True, True]

How does the observed behavior makes sense and how can I achieve the desired behavior?

See Question&Answers more detail:os

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

1 Answer

Each list is being evaluated as a whole. [False, True] is True, and so is [True, True] because only an empty list is False.

and returns the last True element, but or returns the first one.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...