Python 的 any 和 all 函数是如何工作的? - How do Python's any and all functions work?

问题:

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?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章