集合(交、并、差、父、子)

list_1 = [1,2,3,4,5,6,7,8,9]
list_1 = set(list_1)
print(list_1)

list_2 = set([2,4,6,8])
print(list_2,"\n")

list_3 = list_1.intersection(list_2) #求list_1与list_2的交集
print("intersection:",list_3)

print("union:",list_1.union(list_2)) #求list_1与list_2的并集

print("difference:",list_1.difference(list_2)) #求list_1与list_2的差集,list_1减去list_2

print("symmetric_difference:",list_1.symmetric_difference(list_2)) #求list_1与list_2的对称差集

print(list_2.issubset(list_1)) #判断list_2是否为list_1的子集

print(list_1.issuperset(list_2)) #判断list_1是否为list_2的父集
posted @ 2017-10-25 10:37  浆糊jun  阅读(368)  评论(0编辑  收藏  举报