Python 简明教程

Python - Sets

Sets in Python

在 Python 中,集合是一个唯一元素的无序集合。与列表或元组不同,集合不允许重复值,即集合中的每个元素都必须唯一。集合是可变的,这意味着在创建集合后,您可以添加或删除项。

In Python, a set is an unordered collection of unique elements. Unlike lists or tuples, sets do not allow duplicate values i.e. each element in a set must be unique. Sets are mutable, meaning you can add or remove items after a set has been created.

集合使用花括号 {} 或内置 set() 函数定义。它们对于成员资格测试、从序列中删除重复项以及执行常见的数学集合运算(如并集、交集和差集)特别有用。

Sets are defined using curly braces {} or the built-in set() function. They are particularly useful for membership testing, removing duplicates from a sequence, and performing common mathematical set operations like union, intersection, and difference.

Creating a Set in Python

在 Python 中创建集合是指定义和初始化唯一元素的集合。这包括指定将成为集合一部分的元素,确保每个元素在集合中唯一。

Creating a set in Python refers to defining and initializing a collection of unique elements. This includes specifying the elements that will be part of the set, ensuring that each element is unique within the set.

可以使用花括号 {} 或 set() 函数在 Python 中创建集合 −

You can create a set in Python using curly braces {} or the set() function −

Using Curly Braces

您可以通过列出大括号内的元素直接定义集合,并用逗号分隔每个元素,如下所示 −

You can directly define a set by listing its elements within curly braces, separating each element by a comma as shown below −

my_set = {1, 2, 3, 4, 5}
print (my_set)

它将产生以下结果 −

It will produce the following result −

{1, 2, 3, 4, 5}

Using the set() Function

或者,您可以通过传递包含要包含在集合中元素的可迭代(如列表或元组)来使用 set() 函数创建集合 −

Alternatively, you can create a set using the set() function by passing an iterable (like a list or a tuple) containing the elements you want to include in the set −

my_set = set([1, 2, 3, 4, 5])
print (my_set)

我们得到了如下输出 −

We get the output as shown below −

{1, 2, 3, 4, 5}

Duplicate Elements in Set

Python 中的集合是唯一元素的无序集合。如果你尝试使用重复元素创建集合,则会自动删除重复元素 −

Sets in Python are unordered collections of unique elements. If you try to create a set with duplicate elements, duplicates will be automatically removed −

my_set = {1, 2, 2, 3, 3, 4, 5, 5}
print (my_set)

获得的结果如下所示 −

The result obtained is as shown below −

{1, 2, 3, 4, 5}

集合可以包含不同数据类型的元素,包括数字、字符串,甚至其他集合(只要它们不可变) −

Sets can contain elements of different data types, including numbers, strings, and even other sets (as long as they are immutable) −

mixed_set = {1, 'hello', (1, 2, 3)}
print (mixed_set)

产生的结果如下 −

The result produced is as follows −

{1, 'hello', (1, 2, 3)}

在 Python 中,集合支持各种基本操作,用于对其元素进行操作。这些操作包括添加和删除元素、检查成员资格以及执行集合特定操作,如并集、交集、差集和对称差集。

In Python, sets support various basic operations that is used to manipulate their elements. These operations include adding and removing elements, checking membership, and performing set-specific operations like union, intersection, difference, and symmetric difference.

Adding Elements in a Set

要将元素添加到集合,可以使用 add() 函数。当您想要将新元素包含到现有集合中时,这很有用。如果集合中已经存在该元素,则集合保持不变 −

To add an element to a set, you can use the add() function. This is useful when you want to include new elements into an existing set. If the element is already present in the set, the set remains unchanged −

my_set = {1, 2, 3, 3}
# Adding an element 4 to the set
my_set.add(4)
print (my_set)

以下是所获得的输出 −

Following is the output obtained −

{1, 2, 3, 4}

Removing Elements from a Set

可以使用 remove() 函数从集合中删除元素。当您希望从集合中消除特定元素时,这很有用。如果不存在该元素,则会引发 KeyError −

You can remove an element from a set using the remove() function. This is useful when you want to eliminate specific elements from the set. If the element is not present, a KeyError is raised −

my_set = {1, 2, 3, 4}
# Removes the element 3 from the set
my_set.remove(3)
print (my_set)

显示的输出如下 −

The output displayed is as shown below −

{1, 2, 4}

或者,如果存在该元素,您可以使用 discard() 函数从集合中删除该元素。与 remove() 不同,如果在集合中未找到该元素,则 discard() 不会引发错误 −

Alternatively, you can use the discard() function to remove an element from the set if it is present. Unlike remove(), discard() does not raise an error if the element is not found in the set −