列表推导式很强大。
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]
网友评论