欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python学习-21 集合 2

程序员文章站 2022-03-25 20:16:56
集合的其他方法 1.交差补集 运行结果: 2.如果交集李没有重复的返回True 运行结果: 3.math 是 English的子集 运行结果: 4.更新(可以更新多个值) 运算结果: ps:不可变集合 运行结果: ......

集合的其他方法

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