码迷,mamicode.com
首页 > 其他好文 > 详细

集合相关操作

时间:2019-02-11 17:11:32      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:add   date   inter   update   move   amp   apple   style   clear   

a=set(‘15awee‘)            #创建可变集合
s=frozenset(‘kokoko‘) #创建不变集合
print(a,s)

b=[‘dsss‘,4455,‘dsss‘] #将列表转换为集合
b1=set(b)
print(b1)

c=set(‘alex‘)
c.add(‘apple‘) #用add方法给集合添加一个元素‘apple‘
c.add(555) #用add方法给集合添加一个元素‘555‘
print(c)

d=set(‘o‘)
d.update(‘apple‘) #update也是添加元素,但此时增加的是四个元素‘a‘,‘p‘,‘l‘,‘e‘
d1=set(‘o‘)
d1.update([122,‘apple‘])#此时增加的是两个元素‘122‘,‘apple‘
print(d,d1)

e=[‘app‘,88,‘xx‘,799]
e1=set(e)
print(e1) #查看e1的元素
e1.remove(‘app‘) #用remove删除一个元素
print(e1)
e1.pop() #随机删除一个元素
print(e1)
e1.clear() #清空集合中的元素
print(e1)
del e1 #删除集合

a=set([1,2,3,4,5])
b=set([4,5,6,7,8])
print(a.union(b))  #并集
print(a | b)

print(a.intersection(b)) #交集
print(a & b)

print(a.difference(b)) #差集
print(a-b)

print(a.symmetric_difference(b)) #对称差集
print(a^b)
 

集合相关操作

标签:add   date   inter   update   move   amp   apple   style   clear   

原文地址:https://www.cnblogs.com/Finance-IT-gao/p/10362208.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!