349. 两个数组的交集|python

列表推导式很强大。

def section(num1,num2):
    # 交集并集差集
    alist = [i for i in num1 if i in num2]
    alist2 = [i for i in num1 if i not in num2]
    alist3 = list(set(num1).union(set(num2)))

    print(alist,alist2,alist3)


if __name__ == '__main__':
    num1 = [4,9,5]
    num2 = [9,4,9,8,4]
    section(num1,num2)

结果:
[4, 9] [5] [4, 5, 8, 9]

上一篇:数据分析第一周


下一篇:Django搭建个人博客(二)