Python 集合差集,并集,交集
2020-02-06 / 大向

Python 集合差集,并集,交集

1、两个集合的差集

正常方式

    ret = []
    for i in a:
        if i not in b:
            ret.append(i)

简化版

    ret = [ i for i in a if i not in b ]

高级版

    ret = list(set(a) ^ set(b))

最终版

    print (list(set(b).difference(set(a)))) # b中有而a中没有的

2、两个list并集

    print (list(set(a).union(set(b))))

2、两个list交集

    print (list(set(a).intersection(set(b))))
xxx
本文链接:https://chenylwork.gitee.io/2020/02/06/Python%20%E9%9B%86%E5%90%88%E5%B7%AE%E9%9B%86%EF%BC%8C%E5%B9%B6%E9%9B%86%EF%BC%8C%E4%BA%A4%E9%9B%86/