python入门教程9--集合

328 阅读1分钟

集合(set)是一个无序的不重复元素序列。
可以使用大括号 { } 或者 set() 函数创建集合

1、添加元素
thisset = set((“a”, “b”, “c”))
thisset.add(“d”)
print(thisset)
{‘a’, ‘b’, ‘c’, ‘d’}

但是如果已经存在相同的对象,那就会不进行操作

2、移除元素
thisset.remove(“a”)

3、计算集合元素个数

len(thisset)

4、清空集合
s.clear()