【Python3】基本数据类型-集合(

2020-01-02 17:58:23 浏览数 (2)

集合,不可重复的列表,可变类型

difference s1中存在,s2中不存在

代码语言:javascript复制
s1 = {'xmzncc','fcc'}
s2 = {'alex','wusir'}
v = s1.difference(s2)
print(v)

s2中存在,s1中不存在

代码语言:javascript复制
s1 = {'xmzncc','fcc','test'}
s2 = {'alex','wusir','xmzncc'}
v = s2.difference(s1)
print(v)

difference_update s1中存在,s2中不存在,然后对s1清空,然后在重新赋值

代码语言:javascript复制
s1 = {'xmzncc','fcc','test'}
s2 = {'alex','wusir','xmzncc'}
s1.difference_update(s2)
print(s1)

symmetric_difference s1与s2bu不同处

代码语言:javascript复制
s1 = {'xmzncc','fcc','test'}
s2 = {'alex','wusir','xmzncc'}
v = s1.symmetric_difference(s2)
print(v)

intersection s1与s2交集处

代码语言:javascript复制
s1 = {'xmzncc','fcc','test'}
s2 = {'alex','wusir','xmzncc'}
v = s1.intersection(s2)
print(v)

union 并集

代码语言:javascript复制
s1 = {'xmzncc','fcc','test'}
s2 = {'alex','wusir','xmzncc'}
v = s1.union(s2)
print(v)

discard 移除

代码语言:javascript复制
s1 = {'xmzncc','fcc','test'}
s1.discard('xmzncc')
print(s1)

update 更新

代码语言:javascript复制
s1 = {"alex",'eric','tony','李泉','李泉11'}
s1.update({'alex','123123','fff'})
print(s1)

0 人点赞