本教程将向您介绍 Python 集合(Set)以及如何使用它们。
译自 What Are Python 'Sets' and How Do You Use Them?...Python 集合(Set)是一种可迭代、可变且不可重复的数据类型。此数据类型非常方便。例如,你需要存储员工 ID 的信息。你肯定不希望这些 ID 在应用程序中重复,因为这可能会导致问题。...Python 包含内置的 set() 函数,可以轻松创建集合,如下所示:
set1 = set([2, 2, 2, 4, 4, 4, 6, 8, 8, 10])
上面你看到的是一个集合,其中包含一个数字列表...假设我们有两个集合:
set1 = {'Tom Sawyer', 'Analog Kid', 'Between The Wheels'}
set2 = {'La Villa Strangiato', '...’}
Set after discard: {‘Tom Sawyer’, ‘Analog Kid’}
这就是 Python 中集合的基础知识。