Python_集合

1.集合相关操作:添加,移除

  集合的特性:唯一性,元素的内容必须是唯一的,不能重复。

  

colors={'','','','绿','',''}
colors=set(colors) #集合声明
print(colors)

colors.add('')#add() 集合添加元素
print(colors)

colors.remove('') #remove() 集合移除元素
print(colors)

colorArray=['','','','绿','','']
colors1=set(colorArray) #数组转集合
print(colors1)

执行结果:

{'黄', '绿', '橙', '红'}
{'黄', '红', '绿', '橙', '青'}
{'红', '绿', '橙', '青'}
{'黄', '绿', '橙', '红'}

原文地址:https://www.cnblogs.com/myfy/p/11468307.html