Python集

时间:2020-02-23 14:43:18  来源:igfitidea点击:

在本教程中,我们将学习Python Set。

Python集合

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集添加元素

在前面的示例中,我们学习了如何直接初始化Python集。
假设我们需要添加要设置的元素,可以使用add()函数来实现。
但是此功能可以添加单个元素。
如果要添加诸如list或者set之类的可迭代元素,则可以使用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集删除元素

有两个函数可以从Python Set中删除元素。
一个是remove()函数,另一个是throw()函数。
如果要删除的元素不在集合中,则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设置操作

您可能熟悉一些数学集合运算,例如并集,交集,差。
我们也可以使用Python set来完成。
现在,我们将学习如何做到这一点。

Python Set联合

合并是合并两个集合的操作。
这意味着,union将创建另一个包含两个集合的所有唯一元素的集合。
例如,{1、2、3、4}和{2、3、5、7}是两组。
如果对它们执行联合操作,则得到{1、2、3、4、5、7}。
我们可以通过使用union()函数来获得它。

Python Set交集

同样,交集是获得两组公共唯一元素的运算。
例如,{1、2、3、4}和{2、3、5、7}是两组。
如果我们与它们相交,则得到{2,3}。
交集操作由交集()函数完成。

Python设置差异

现在,差异运算将比较两个集合并创建一个新集合,其中包含集合A中集合B中不常见的项。
假设我们有两个集合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} #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])
>>>