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 设置
假设你有一个列表,你只需要列表中的独特项目,你可以使用Python Set。同样,如果你只需要从输入中获得独特项目,Python set可以帮助你做到这一点,你可以从中添加或删除项目,你可以通过将元素放置在弯曲的框架之间来初始化一个集合,就像其他序列一样,一个集合可以有多个数据类型的元素。
1#set containing single data-type
2set1 = {1, 2, 3, 4, 2, 3, 1}
3print(set1)
4
5#set containing multiple data-type
6set2 = {1, 2, 3, (1, 2, 3), 2.45, "Python", 2, 3}
7print(set2)
8
9#creating a set from a list
10theList = [1, 2, 3, 4, 2, 3, 1]
11theSet = set(theList)
12print(theSet)
产量将是
1================== RESTART: /home/imtiaz/set1.py ==================
2set([1, 2, 3, 4])
3set([1, 2, 3, 2.45, 'Python', (1, 2, 3)])
4set([1, 2, 3, 4])
5>>>
在 Python 集合中添加元素
在之前的例子中,我们学到了如何直接初始化 Python 设置。假设我们需要将元素添加到设置中,我们可以通过使用 add() 函数来做到这一点,但是这个函数可以添加单个元素。
1#initialize an empty set
2theSet = set()
3
4#add a single element using add() function
5theSet.add(1)
6theSet.add(2)
7theSet.add(3)
8theSet.add(2)
9#add another data-type
10theSet.add('hello')
11
12#add iterable elements using update() function
13theSet.update([1,2,4,'hello','world']) #list as iterable element
14theSet.update({1,2,5}) #set as iterable element
15print(theSet)
以下代码的输出将是
1================== RESTART: /home/imtiaz/set_new.py ==================
2set([1, 2, 3, 4, 5, 'world', 'hello'])
3>>>
删除从 Python 集中的元素
有两个函数可以从Python集中删除元素,一个是 remove(),另一个是 discard() 函数. 如果你试图删除的元素不在集中,则 remove() 函数会为此提出例外,但排除函数不会做任何此类的事情。
1theSet = {1,2,3,4,5,6}
2
3#remove 3 using discard() function
4theSet.discard(3)
5print(theSet)
6
7#call discard() function again to remove 3
8theSet.discard(3) #This won't raise any exception
9print(theSet)
10
11#call remove() function to remove 5
12theSet.remove(5)
13print(theSet)
14
15#call remove() function to remove 5 again
16theSet.remove(5) #this would raise exception
17print(theSet) #this won't be printed
你会发现结果是这样的,
1================== RESTART: /home/imtiaz/set_del.py ==================
2set([1, 2, 4, 5, 6])
3set([1, 2, 4, 5, 6])
4set([1, 2, 4, 6])
5
6Traceback (most recent call last):
7 File "/home/imtiaz/set_del.py", line 16, in
8 theSet.remove(5) #this would raise exception
9KeyError: 5
10>>>
Python 操作系统
你可能熟悉一些数学集操作,如联盟,交叉,差异,我们也可以使用Python集。
Python 设置联盟
例如, {1, 2, 3, 4} 和 {2, 3, 5, 7} 是两个集合的操作. 如果我们对它们进行联合操作,我们将得到 {1, 2, 3, 4, 5, 7}. 我们可以通过使用 union() 函数来获得这一点。
Python 设置交叉
再一次,交叉是获得两个集合的共同独特元素的操作,例如, {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() 函数完成的。
1A = {1, 2, 3, 4} #initializing set A
2B = {2, 3, 5, 7} #initializing set B
3
4union_operation = A.union(B)
5
6print("A union B :")
7print(union_operation)
8
9intersection_operation = A.intersection(B)
10
11print("A intersection B :")
12print(intersection_operation)
13
14difference_operation = A.difference(B)
15
16print("A-B :")
17print(difference_operation)
18
19difference_operation = B.difference(A)
20print("B-A :")
21print(difference_operation)
你得到的输出将是这样的
1================== RESTART: /home/imtiaz/set_op.py ==================
2A union B :
3set([1, 2, 3, 4, 5, 7])
4A intersection B :
5set([2, 3])
6A-B :
7set([1, 4])
8B-A :
9set([5, 7])
10>>>
所以,这就是今天的一切。希望你对Python Set了解得很好。对于任何进一步的查询,你只能在评论框中写下你的查询。