python 交集

您所在的位置:网站首页 python交集怎么算 python 交集

python 交集

#python 交集| 来源: 网络整理| 查看: 265

python 交集

The intersection (A∩B) of two sets A and B is the set that contains all the elements common between both the sets.

交点两组A和B的(A∩B)是集合包含集之间既所有元素常见。

We can perform an intersection between multiple sets too. Following image shows the set intersection example between two sets and three sets.

我们也可以在多个集合之间执行交集。 下图显示了两个集合和三个集合之间的集合相交示例。

Set Intersection

Set Intersection

设置相交

Python设置交集 (Python set intersection)

In Python, we can use set class intersection() function to get the intersection of two sets.

在Python中,我们可以使用set类intersection()函数来获取两个集合的交集。

Let’s look at an example of the intersection between two sets.

让我们看一下两个集合之间的交集的示例。

set1 = set('abcde') set2 = set('ae') print(set1) print(set2) # two sets intersection print(set1.intersection(set2))

Output:

输出:

{'a', 'e', 'b', 'd', 'c'} {'a', 'e'} {'a', 'e'} Python Two Sets Intersection

Python Two Sets Intersection

Python两集相交

The intersection() function takes multiple set arguments too. Let’s look at another example of the intersection between multiple sets.

交集()函数也接受多个设置参数。 让我们看一下多个集合之间交集的另一个示例。

set1 = set('abcde') set2 = set('ae') set3 = {'a'} print(set1.intersection(set2, set3))

Output: {'a'}

输出: {'a'}

Python Multiple Set Intersection

Python Multiple Set Intersection

Python多集交集

设置不带参数的交集 (Set intersection without arguments)

We can call intersection() function without an argument too. In this case, a copy of the set will be returned.

我们也可以调用不带参数的交集()函数。 在这种情况下,将返回该集合的副本。

Let’s see if the returned copy refers to the same set or a different one.

让我们看看返回的副本是指相同的集合还是不同的集合。

set1 = set('ab') set4 = set1.intersection() print(set4) # check if shallow copy or deep copy set1.add('0') set1.remove('a') print(set1) print(set4)

Output:

输出:

{'b', 'a'} {'0', 'b'} {'c', 'b', 'a'} GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

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

python 交集



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3