python 交集 并集 差集

def diff(listA, listB):
    # 求交集的两种方式
    retA = [i for i in listA if i in listB]
    retB = list(set(listA).intersection(set(listB)))
    #print("retA is: ", retA)
    #print("retB is: ", retB)
    # 求并集
    retC = list(set(listA).union(set(listB)))
    #print("retC1 is: ", retC)
    # 求差集,在B中但不在A中
    retD = list(set(listB).difference(set(listA)))
    print("retD is: ", retD)
    retE = [i for i in listB if i not in listA]
    #print("retE is: ", retE)
listA = [1,2,3,4,5]
listB = [3,4,5,6,7]

diff(listA, listB)

猜你喜欢

转载自www.cnblogs.com/wangcongxing/p/12220590.html