标签:union 并集 文件 字符 join pre lis section 取出
list1 = set([1, 2, 3, 4, 5, 6, 5, 5, 5])
list2 = set([11, 2, 36, ‘a‘, 5, 6, 5, 5, 5])
list3 = set([1, 2, 3])
list4 = set([‘a‘, ‘b‘, ‘c‘])
求交集
print(list1.intersection(list2))
print(list1&list2)
输出{2, 5, 6}
求并集
print(list1.union(list2))
print(list1 | list2)
输出{1, 2, 3, 4, 5, 6, 36, ‘a‘, 11}
求差集
print(list1.difference(list2)) #在list1不在list2
print(list1 - list2)
输出{1, 3, 4}
print(list2.difference(list1)) #在list2不在list1
print(list2 - list1)
输出{‘a‘, 11, 36}
子集(无运算符)
print(list1.issubset(list2))
输出False
print(list3.issubset(list1))
输出True
对称差集:两个集合互相没有的取出来放一起
print(list1.symmetric_difference(list2))
输出{1, 3, 36, 4, 11, ‘a‘}
isdisjoint:Return True if two sets have a null intersection #两个集合没有交集返回true,否则返回false
print(list3.isdisjoint(list1))
输出False
print(list1.isdisjoint(list4))
输出True
标签:union 并集 文件 字符 join pre lis section 取出
原文地址:http://www.cnblogs.com/yum777/p/6679390.html