代码
colletion1 = set([1,2,3,4]) #必须是set,才能进行集合运算
colletion2 = set([3,4,5])
one = colletion1 & colletion2 #求交集
print("one:%s" % one)
two = colletion1 - colletion2 #求差集(conlletion2的补集)
print("two:%s" % two)
three = colletion1 | colletion2 #求并集
print("thress:%s" % three)
four = set([3,4,5])
print(colletion2 == four) #判断是否相等
print(colletion1 == colletion2) #判断是否相等
print(1 in colletion1) # in只能判断单个元素是否在集合中,不可以判断某个集合是否是另一个集合的子集
five = set([1,2,3])
print(colletion1 > five) #可以用这种方法判断子集
print(colletion2 > five) #可以用这种方法判断子集
结果
one:{3, 4}
two:{1, 2}
thress:{1, 2, 3, 4, 5}
True
False
True
True
False