python集合_Python集

python集合

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 String。 您可以从这里学习。

Python集 (Python Set)

Python Set is an unordered collection of unique elements. Suppose you have a list and you need only the unique items of the list you can use Python Set. Similarly, if you need only unique items from input, Python set can help you to do so. You can add or delete items from it.

Python Set是唯一元素的无序集合。 假设您有一个列表,并且只需要可以使用Python Set的列表中的唯一项。 同样,如果您只需要输入中的唯一项,则Python set可以帮助您做到这一点。 您可以从中添加或删除项目。

You can initialize a set by placing elements in between curly braces. Like other sequences, one set can have elements of multiple data-types. Moreover, you can also create a set from a list by using set() function. The following example will give you some idea about initializing a 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)

The output will be

输出将是

================== 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集添加元素 (Adding Elements to Python Set)

In previous example, we learned how to initialize Python set directly. Suppose we need to add element to set, we can do so by using add() function. But this function can add a single element. If you want to add iterable elements like list or set, you can do so by using update() function. The following example will help you understand the thing

在前面的示例中,我们学习了如何直接初始化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)

The output of the following code will be

以下代码的输出将是

================== RESTART: /home/imtiaz/set_new.py ==================
set([1, 2, 3, 4, 5, 'world', 'hello'])
>>>

从Python集删除元素 (Remove Elements from Python Set)

There are two functions to remove elements from Python Set. One is remove() and another is discard() function. If the element you are trying to remove is not in the set, the remove() function will raise exception for this. But the discard function will not do anything like this. The following code will show you those

有两个函数可以从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

You will find the output be like,

您会发现输出像

================== 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 Operations)

You might be familiar with some mathematical set operations like union, intersection, difference. We can also do those using Python set. Now, we will learn how to do that.

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

Python Set联合 (Python Set Union)

Union is the operation to merge two sets. That means, union will create another set that contains all unique elements of two sets. For example, {1, 2, 3, 4} and {2, 3, 5, 7} are two sets. If we do union operation over them, we get {1, 2, 3, 4, 5, 7}. We can obtain this by using union() function.

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

Python Set交集 (Python Set Intersection)

Again, intersection is the operation to get the common unique elements of two sets. For example, {1, 2, 3, 4} and { 2, 3, 5, 7} are two sets. If we intersect them, we get, {2, 3}. The intersection operation is done by intersection() function.

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

Python设置差异 (Python Set Difference)

Now, difference operation compares two sets and creates a new set containing items from set A which are not common in set B. Suppose, we have two sets, A = {1, 2, 3, 4} and B = {2, 3, 5, 7}. Then, A – B operation will generate {1, 4}. Moreover, B – A will generate {5, 7}. The difference operation is done by difference() function..

现在,差异运算将比较两个集合,并创建一个新集合,其中包含集合A中集合B中不常见的项目。假设我们有两个集合,A = {1,2,3,4}和B = {2,3 ,5,7}。 然后,A – B操作将生成{1,4}。 此外,B – A将生成{5,7}。 差异操作通过difference()函数完成。

The following code will give you idea about how to do these set operation in python programming.

以下代码将使您了解如何在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)

The output you get will be like this

您得到的输出将是这样的

================== 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])
>>>

So, that’s all for today. Hope that you learned well about Python Set. For any further query you can just write your query in the comment box. We will answer you.

所以,今天就这些。 希望您对Python Set有所了解。 对于任何其他查询,您只需在注释框中输入查询即可。 我们将回答您。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/14394/python-set

python集合

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值