gpt4 book ai didi

python - 确定集合列表是否共享数据

转载 作者:行者123 更新时间:2023-11-28 22:13:41 26 4
gpt4 key购买 nike

给定一组集合,如果所有集合都不共享数据,您将如何测试:

例如:

[set((1,2)),set((3,)),set((4,)),set((5,))] would be True

但是

[set((1,2)),set((2,)),set((4,)),set((5,))] would be False

最佳答案

一种快速的方法是对集合的大小求和并将其与并集的大小进行比较:

def no_common_elements(sets):
return sum(len(s) for s in sets) == len(set.union(*sets))

print(no_common_elements([set((1, 2)), set((3,)), set((4,)), set((5,))]))
# True
print(no_common_elements([set((1, 2)), set((2,)), set((4,)), set((5,))]))
# False

关于python - 确定集合列表是否共享数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53766633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com