集合的其他方法
1.交差补集
math = {'xm','xh','xg','xx'}
english ={'xm','xh','dm','john'} print(math.symmetric_difference(english))
运行结果:
{'dm', 'john', 'xx', 'xg'} Process finished with exit code 0
2.如果交集李没有重复的返回True
math = {'xm','xh','xg','xx'}
english ={'xd','xb','dm','john'} print(math.isdisjoint(english))
运行结果:
True Process finished with exit code 0
3.math 是 English的子集
math = {'xm','xh','xg','xx'}
english ={'xd','xb','dm','john'} print(math.issubset(english))
运行结果:
False (因为不是,所以返回false) Process finished with exit code 0
4.更新(可以更新多个值)
math = {'xm','xh','xg','xx'}
english ={'xd','xb','dm','john'}
math.update(english)
print(math)
运算结果:
{'xm', 'xh', 'john', 'xb', 'xd', 'dm', 'xx', 'xg'} Process finished with exit code 0
ps:不可变集合
s = frozenset('hello')
print(s)
运行结果:
frozenset({'e', 'l', 'h', 'o'}) Process finished with exit code 0