Python_集合
1.集合相关操作:添加,移除
集合的特性:唯一性,元素的内容必须是唯一的,不能重复。
colors={'红','橙','黄','绿','红','黄'}
colors=set(colors) #集合声明
print(colors)
colors.add('青')#add() 集合添加元素
print(colors)
colors.remove('黄') #remove() 集合移除元素
print(colors)
colorArray=['红','橙','黄','绿','红','黄']
colors1=set(colorArray) #数组转集合
print(colors1)
执行结果:
{'黄', '绿', '橙', '红'}
{'黄', '红', '绿', '橙', '青'}
{'红', '绿', '橙', '青'}
{'黄', '绿', '橙', '红'}
浙公网安备 33010602011771号