Python集合:深入理解数据结构与高效应用

Python 集合详解
Python 集合(Set)是一个无序且元素唯一的集合。当您需要从列表中提取唯一元素,或者仅从输入中获取不重复的数据时,Python 集合是理想的选择。您可以方便地向集合中添加或删除元素。
初始化集合可以通过将元素放置在花括号 {}
之间实现。与列表等其他序列类似,集合可以包含多种数据类型的元素。此外,您还可以使用 set()
函数从现有列表中创建集合。以下示例将为您展示如何初始化和创建集合:
# 包含单一数据类型的集合
set1 = {1, 2, 3, 4, 2, 3, 1}
print(set1)
# 包含多种数据类型的集合
set2 = {1, 2, 3, (1, 2, 3), 2.45, "Python", 2, 3}
print(set2)
# 从列表创建集合
theList = [1, 2, 3, 4, 2, 3, 1]
theSet = set(theList)
print(theSet)
以下是上述代码的输出结果:
================== RESTART: /home/imtiaz/set1.py ==================
{1, 2, 3, 4}
{1, 2, 3, 2.45, 'Python', (1, 2, 3)}
{1, 2, 3, 4}
>>>
向 Python 集合中添加元素
在前面的例子中,我们学习了如何直接初始化 Python 集合。如果您需要向集合中添加新元素,可以使用 add()
函数。但请注意,add()
函数一次只能添加一个元素。如果您想添加可迭代的元素(如列表或另一个集合),则应使用 update()
函数。以下示例将帮助您理解这两种添加方式:
# 初始化一个空集合
theSet = set()
# 使用 add() 函数添加单个元素
theSet.add(1)
theSet.add(2)
theSet.add(3)
theSet.add(2) # 重复元素不会被添加
# 添加其他数据类型
theSet.add('hello')
# 使用 update() 函数添加可迭代元素
theSet.update([1,2,4,'hello','world']) # 列表作为可迭代元素
theSet.update({1,2,5}) # 集合作为可迭代元素
print(theSet)
以下是上述代码的输出结果:
================== RESTART: /home/imtiaz/set_new.py ==================
{1, 2, 3, 4, 5, 'world', 'hello'}
>>>
从 Python 集合中删除元素
从 Python 集合中删除元素主要有两种方法:使用 remove()
函数或 discard()
函数。它们的主要区别在于:如果尝试删除的元素不在集合中,remove()
函数会抛出 KeyError
异常,而 discard()
函数则不会执行任何操作,也不会报错。以下代码将演示这两种函数的使用:
theSet = {1,2,3,4,5,6}
# 使用 discard() 函数删除元素 3
theSet.discard(3)
print(theSet)
# 再次调用 discard() 函数删除元素 3
theSet.discard(3) # 这不会引发任何异常,因为 3 已经不存在
print(theSet)
# 调用 remove() 函数删除元素 5
theSet.remove(5)
print(theSet)
# 再次调用 remove() 函数删除元素 5
theSet.remove(5) # 这将引发异常,因为 5 已经不存在
print(theSet) # 这行代码将不会被执行
您将会看到如下输出结果:
================== RESTART: /home/imtiaz/set_del.py ==================
{1, 2, 4, 5, 6}
{1, 2, 4, 5, 6}
{1, 2, 4, 6}
Traceback (most recent call last):
File "/home/imtiaz/set_del.py", line 16, in
theSet.remove(5) # this would raise exception
KeyError: 5
>>>
Python 集合操作
您可能对数学中的集合操作,如并集、交集和差集比较熟悉。Python 集合也提供了这些功能。现在,我们来学习如何利用 Python 实现这些集合操作。
Python 集合的并集
集合的并集操作是将两个集合合并,生成一个包含两个集合中所有唯一元素的新集合。例如,如果集合 A 是 {1, 2, 3, 4}
,集合 B 是 {2, 3, 5, 7}
。对它们进行并集操作后,我们将得到 {1, 2, 3, 4, 5, 7}
。在 Python 中,我们可以通过使用 union()
函数来实现并集。
Python 集合的交集
集合的交集操作是找出两个集合中共同的唯一元素,并生成一个新集合。例如,如果集合 A 是 {1, 2, 3, 4}
,集合 B 是 {2, 3, 5, 7}
。对它们进行交集操作后,我们将得到 {2, 3}
。交集操作由 intersection()
函数完成。
Python 集合的差集
差集操作比较两个集合,并创建一个新集合,其中包含第一个集合中有而第二个集合中没有的元素。假设我们有两个集合:A = {1, 2, 3, 4}
和 B = {2, 3, 5, 7}
。那么,A – B 操作将生成 {1, 4}
。同样地,B – A 将生成 {5, 7}
。差集操作由 difference()
函数完成。以下代码将为您展示如何在 Python 编程中执行这些集合操作:
A = {1, 2, 3, 4} # 初始化集合 A
B = {2, 3, 5, 7} # 初始化集合 B
union_operation = A.union(B)
print("A 并集 B :")
print(union_operation)
intersection_operation = A.intersection(B)
print("A 交集 B :")
print(intersection_operation)
difference_operation = A.difference(B)
print("A - B :")
print(difference_operation)
difference_operation = B.difference(A)
print("B - A :")
print(difference_operation)
您将得到如下输出结果:
================== RESTART: /home/imtiaz/set_op.py ==================
A 并集 B :
{1, 2, 3, 4, 5, 7}
A 交集 B :
{2, 3}
A - B :
{1, 4}
B - A :
{5, 7}
>>>
今天的 Python 集合教程就到这里。希望您对 Python 集合有了深入的理解和掌握。如果您有任何疑问,请在评论区留言,我们会尽快为您解答。参考资料:Python 官方文档