首页 › 参考手册 › 内置函数 › set()

Python set() 函数

描述

创建集合,自动去除重复元素。创建空集合必须用 set(),{} 是空字典。

语法

set([iterable])

参数

参数说明
iterable可迭代对象,可选

返回值

新集合

实例

print(sorted(set("hello")))
print(set([1, 2, 2, 3]))
print(type({}), type(set()))
以上实例输出结果为:
['e', 'h', 'l', 'o']
{1, 2, 3}
<class 'dict'> <class 'set'>