Intersection() 函式 Python


在本文中,我們將學習如何針對給定的任何集合執行 intersection() 函式。根據數學,交集表示找出兩個集合中的交集元素。

語法

<set name>.intersection(<set a1> <set a2> ……..)

返回值 

作為引數傳遞的集合中的公共元素。

示例

set_1 = {'t','u','t','o','r','i','a','l'}
set_2 = {'p','o','i','n','t'}
set_3 = {'t','u','t'}
#intersection of two sets
print("set1 intersection set2 : ", set_1.intersection(set_2))
# intersection of three sets
print("set1 intersection set2 intersection set3 :", set_1.intersection(set_2,set_3))

輸出

set1 intersection set2 : {'i', 'o', 't'}
set1 intersection set2 intersection set3 : {'t'}

說明 

在這裡,透過直譯器進行搜尋以找出公共元素,並將這些元素作為集合返回到各自的引用。

結論

在本文中,我們學習瞭如何在 Python 3.x 或更早版本中實現和使用交集函式。

更新於: 2020-7-3

174 次瀏覽

開啟你的事業

完成課程以獲得認證

開始
廣告
© . All rights reserved.