問題:
I'm trying to understand how the any()
and all()
Python built-in functions work.我試圖瞭解any()
和all()
Python 內置函數是如何工作的。
I'm trying to compare the tuples so that if any value is different then it will return True
and if they are all the same it will return False
.我正在嘗試比較元組,以便如果任何值不同,則返回True
,如果它們都相同,則返回False
。 How are they working in this case to return [False, False, False]?在這種情況下,他們如何工作以返回 [False, False, False]?
d
is a defaultdict(list)
. d
是一個defaultdict(list)
。
print d['Drd2']
# [[1, 5, 0], [1, 6, 0]]
print list(zip(*d['Drd2']))
# [(1, 1), (5, 6), (0, 0)]
print [any(x) and not all(x) for x in zip(*d['Drd2'])]
# [False, False, False]
To my knowledge, this should output據我所知,這應該輸出
# [False, True, False]
since (1,1) are the same, (5,6) are different, and (0,0) are the same.因爲 (1,1) 相同,(5,6) 不同,(0,0) 相同。
Why is it evaluating to False for all tuples?爲什麼它對所有元組都評估爲 False?
解決方案:
參考一: https://stackoom.com/question/1JM5i參考二: How do Python's any and all functions work?