集合是无序的,且唯一无重复的
#空集合 v=set() #添加 v={1,2} v.add('sky') v.add('sky') print(v) #删除 v={1,2,'sky'} v.discard('sky') print(v)
#update 批量添加
v={1,2,'sky'}
v.update({11,22,33})
print(v)
交集
#交集 会生成新的值 可以是列表 v={1,2,'sky'} result=v.intersection({1,'sky','哈哈'}) print(result)
{1, 'sky'}
并集
#并集 会生成新的值 v={1,2,'sky'} result=v.union({1,'sky','哈哈'}) print(result)
{1, 2, 'sky', '哈哈'}
差集
#差集 会生成新的值 v={1,2,'sky'} result=v.difference({1,'sky','哈哈'}) #v中有且{1,'sky','哈哈'}没有 print(result)
{2}
for 循环
v={1,2,'sky'} for i in v: print(i)
1
2
sky
集合的嵌套
info={1,2,3,4,5,True,'哈哈',None,(1,2,3),[1,2,3],{'k1':'v1'},{1,2,3}} #不能存放列表,字典,集合(可变的是不能放到集合)