While experimenting with the reduce function, I've observed behavior that I can't explain to myself. Say, there are 2 lists:
a = ["a", "b", "c", "z"]
b = ["b", "z", "a"]
I woud like to verify whether all elements of the list b are in the list a using reduce . So, I try:
reduce(lambda x,y: (x in a) and (y in a), b)
and get False instead of expected True .
So, why do I get False ?
PS: I know that there are other ways to verify if all list's elements are in another list, fi using sets and issuperset . I just wonder why the reduce function works this way.
Your code:
b = ["b", "z", "a"]
reduce(lambda x,y: (x in a) and (y in a), b)
is equivalent to:
(("b" in a) and ("z" in a)) in a and ("a" in a)
which calculates to:
(True in a) and ("a" in a)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.