美文网首页
Python使用reduce()函数计算多个集合的并集与交集

Python使用reduce()函数计算多个集合的并集与交集

作者: 还是那个没头脑 | 来源:发表于2019-08-23 15:14 被阅读0次
from functools import reduce

a = [{1, 2, 3, 5}, {2, 3, 4, 5}, {3, 4, 5}]
def intersect(x,y):
    return x | y

res = reduce(intersect,a)
print(res)

{1, 2, 3, 4, 5}

def union(x,y):
    return x & y
res = reduce(union,a)
print(res)

{3, 5}

相关文章

网友评论

      本文标题:Python使用reduce()函数计算多个集合的并集与交集

      本文链接:https://www.haomeiwen.com/subject/bohtectx.html