定义
set 无序 去重 不支持角标
s1 = {10, 20, 30, 40, 50}
print(s1)
s2 = {10, 30, 20, 10, 30, 40, 30, 50}
print(s2)
s3 = set('abcdefg') #注意
print(s3)
s4 = set()
print(type(s4))
s5 = {}
print(type(s5)) #注意
'''
{50, 20, 40, 10, 30}
{50, 20, 40, 10, 30}
{'g', 'a', 'b', 'd', 'e', 'f', 'c'}
<class 'set'>
<class 'dict'>
'''
增
add() 若增加存在的数据 不进行任何操作
s1 = {10, 20, 30, 40, 50}
s1.add(110)
print(s1)
删
remove('xx') 删除指定的数据 没有del() 因为无序无角标
s1.remove(xx) 数据若不存在报错
discard() 这个不存在的话就不会报错,不做任何操作 好使
pop() 随机删除单个数据,并返回
改
update(序列或者字符串)
s1 = {"apple", "banana", "cherry"}
s1.update('10')
s1.update([10,20])
print(s1)
# {10, 'banana', '1', 20, 'apple', 'cherry', '0'}
查
in:判断数据在集合序列
not in:判断数据不在集合序列