Manual
Return a new set object, optionally with elements taken from iterable. set is a built-in class. See set and Set Types — set, frozenset for documentation about this class.
For other containers see the built-in frozenset, list, tuple, and dict classes, as well as the collections module.
直译
返回一个新的set对象,可以从iterable获取元素。set是内建类,详情见集合类型—set、frozenset
对于其他容器类,可参考内建frozenset、list、tuple和dict类,以及collections模块
实例
>>> set([i for i in range(20)])
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
>>> a = ['Tom', 'Jack', 'Rose', 'Tom']
>>> set(a)
{'Tom', 'Jack', 'Rose'}
>>> for i in set(a):
print(i)
Tom
Jack
Rose
本文详细介绍了 Python 中 set 类型的基本用法,包括如何创建一个新的 set 对象,并从可迭代对象中获取元素。同时,文章提供了实用的代码示例,如使用 set 去重列表中的元素,并展示了如何遍历 set 中的内容。
1399

被折叠的 条评论
为什么被折叠?



