In this tutorial we are going to learn Python Set. In our previous article we learnt about Python String. You can learn it from here.
Python Set 是唯一元素的无序集合。假设您有一个列表,并且您只需要列表中唯一的项目,您可以使用 Python Set。同样,如果您只需要输入中的唯一项,Python set 可以帮助您做到这一点。您可以从中添加或删除项目。您可以通过将元素放在花括号之间来初始化集合。与其他序列一样,一组可以包含多种数据类型的元素。此外,您还可以使用以下命令从列表创建集合set()功能。以下示例将为您提供有关初始化集合的一些想法。
#set containing single data-type
set1 = {1, 2, 3, 4, 2, 3, 1}
print(set1)
#set containing multiple data-type
set2 = {1, 2, 3, (1, 2, 3), 2.45, "Python", 2, 3}
print(set2)
#creating a set from a list
theList = [1, 2, 3, 4, 2, 3, 1]
theSet = set(theList)
print(theSet)
输出将是
================== RESTART: /home/imtiaz/set1.py ==================
set([1, 2, 3, 4])
set([1, 2, 3, 2.45, 'Python', (1, 2, 3)])
set([1, 2, 3, 4])
>>>
在前面的示例中,我们学习了如何直接初始化Python集合。假设我们需要向集合中添加元素,我们可以使用add()功能。但这个函数可以添加单个元素。如果您想添加可迭代元素(例如列表或集合),可以使用update()功能。下面的例子将帮助你理解这个事情
#initialize an empty set
theSet = set()
#add a single element using add() function
theSet.add(1)
theSet.add(2)
theSet.add(3)
theSet.add(2)
#add another data-type
theSet.add('hello')
#add iterable elements using update() function
theSet.update([1,2,4,'hello','world']) #list as iterable element
theSet.update({1,2,5}) #set as iterable element
print(theSet)
以下代码的输出将是
================== RESTART: /home/imtiaz/set_new.py ==================
set([1, 2, 3, 4, 5, 'world', 'hello'])
>>>
有两个函数可以从 Python Set 中删除元素。一是remove()另一个是丢弃()功能。如果您尝试删除的元素不在集合中,remove() 函数将引发异常。但丢弃函数不会做这样的事情。下面的代码将向您展示这些
theSet = {1,2,3,4,5,6}
#remove 3 using discard() function
theSet.discard(3)
print(theSet)
#call discard() function again to remove 3
theSet.discard(3) #This won't raise any exception
print(theSet)
#call remove() function to remove 5
theSet.remove(5)
print(theSet)
#call remove() function to remove 5 again
theSet.remove(5) #this would raise exception
print(theSet) #this won't be printed
你会发现输出是这样的,
================== RESTART: /home/imtiaz/set_del.py ==================
set([1, 2, 4, 5, 6])
set([1, 2, 4, 5, 6])
set([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 集来完成这些操作。现在,我们将学习如何做到这一点。
并集是合并两个集合的操作。这意味着,union 将创建另一个集合,其中包含两个集合的所有唯一元素。例如,{1,2,3,4}和{2,3,5,7}是两个集合。如果我们对它们进行并集运算,我们会得到{1,2,3,4,5,7}。我们可以通过使用来获得它union()功能。
同样,交集是获取两个集合的共同唯一元素的操作。例如,{1,2,3,4}和{2,3,5,7}是两个集合。如果我们将它们相交,我们就会得到 {2, 3}。相交操作是通过以下方式完成的路口()功能。
现在,差异操作比较两个集合并创建一个新集合,其中包含集合 A 中的项目,这些项目在集合 B 中不常见。假设我们有两个集合,A = {1, 2, 3, 4} 和 B = {2, 3 , 5, 7}。然后,A - B 运算将生成{1, 4}。此外,B - A 将生成 {5, 7}。差分运算是通过不同之处()函数...以下代码将让您了解如何在Python编程中执行这些集合操作。
A = {1, 2, 3, 4} #initializing set A
B = {2, 3, 5, 7} #initializing set B
union_operation = A.union(B)
print("A union B :")
print(union_operation)
intersection_operation = A.intersection(B)
print("A intersection 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 union B :
set([1, 2, 3, 4, 5, 7])
A intersection B :
set([2, 3])
A-B :
set([1, 4])
B-A :
set([5, 7])
>>>
那么,这就是今天的全部内容。希望您很好地了解了 Python Set。对于任何进一步的疑问,您可以在评论框中写下您的疑问。我们将为您解答。参考:官方文档