Python - 列印所有子列表中的公共元素


Python 語言主要由不同的資料結構組成,其中列表資料結構是最常用的。列表可以儲存不同資料型別的元素,並且一旦賦值,就不能更改。列表甚至可以在方括號“[]”內包含其他列表。當定義兩個列表在某些平臺上工作時,它們之間可能存在某些關係,並且使用此程式碼,Python 程式可以實現此功能。

列印子列表中的公共元素

假設一個包含 10 個元素的列表,它也可以表示為子列表的形式,例如一個子列表包含 3 個元素,另一個子列表包含剩餘的 7 個元素。

語法

list1 = [[1, 2, 3], [4, 5, 6, 7, 8, 9, 10]] 

基本語法包含列表資料結構以及子列表。

方法

方法 1 - 使用遞迴函式

方法 2 - 使用 reduce 函式

方法 3 - 使用迭代方法

方法 1:使用遞迴函式列印所有子列表中公共元素的 Python 程式

藉助遞迴函式列印列表子列表中的公共元素,該函式自身遞迴呼叫。

演算法

  • 步驟 1 - 建立函式 common_elements()。

  • 步驟 2 - 當列表的長度等於 1 時,它設定列表的第一個元素。

  • 步驟 3 - 否則,它設定交集函式以使用切片方法匹配子列表以查詢公共元素。

  • 步驟 3 - 它返回包含公共元素的語句。

示例

#function is defined with a list argument
def common_elements(list1):
	if len(list1) == 1:
		return set(list1[0])
	else:
		return set(list1[0]).intersection(common_elements(list1[1:]))
#initializing the sublist inside the list data structure
list1 = [[2,3,4,5], [2,5], [2,10,5], [2,8,5,6], [2,10,9,5]]
common_elements = common_elements(list1)
#the length of the list should be greater than 0
if len(common_elements)>0:
	print("The common elements in the given sublists is:",common_elements)
else:
	print("The given list does not contain common elements! ")

輸出

The common elements in the given sublist is: {2, 5}

方法 2:使用 reduce 函式列印所有子列表中公共元素的 Python 程式

在無需任何複雜步驟即可列印公共元素的方法中,匯入 functools 模組以使用 reduce 函式。

演算法

  • 步驟 1 - 匯入所需的模組。

  • 步驟 2 - 使用子列表初始化列表資料結構,以查詢它們之間的公共元素。

  • 步驟 3 - 此問題的核心功能由 reduce 和交集函式實現。

  • 步驟 4 - map 函式簡單地對映元素並將每個子列表轉換為集合。

  • 步驟 5 - 根據定義的輸入,print 語句將返回子列表中的公共元素。

示例

#importing the module
from functools import reduce
#initializing the sublist inside the list data structure
list1 = [[2,3,4,5], [2,5], [2,10,5], [2,8,5,6], [2,10,9,5]]
#with the help of the intersection function to match the elements among sublists
common_elements = reduce(set.intersection,map(set,list1))
#the length of the list should be greater than 0
if len(common_elements)>0:
	print("The common elements in the given sublists is:",common_elements)
else:
	print("The given list does not contain common elements! ")

輸出

The common elements in the given sublist is: {2, 5}

方法 3:使用 for 迴圈迭代方法列印公共元素的 Python 程式

遍歷列表使輸出列印子列表之間所有可能的公共元素。

演算法

  • 步驟 1 - 建立一個帶有一個列表引數的函式。

  • 步驟 2 - 然後建立一個空集,並將陣列的第一個元素作為 list1[0]。

  • 步驟 3 - 使用 for 迴圈進行迭代。

  • 步驟 4 - 驗證條件,即長度是否大於零。

  • 步驟 5 - 呼叫函式 common_element,並列印輸出語句。

示例

#defining the function with one list argument
def common_element(list1):
#creating an empty set and taking the first element of the array
	reference_set=set(list1[0])
#for is used to iterate through the given list
	for num in range(1,len(list1)):
#Adding the common element from the list to the created set
		fresh_set=set(tuple(list1[num]))
		reference_set&=fresh_set
#To check whether the length of the list greater than 0
	if len(reference_set) > 0:
		print("The common elements in the given sublists is:",reference_set)
	else:
		print("The given list does not contain common elements! ")
#initializing the list of sublists 
list1 = [[11,3,12,5], [12,11], [12,11,5], [12,11,5,6], [12,11,9,5]]
#calling the function to print the common elements
common_element(list1)

輸出

The common elements in the given sublist is: {11, 12}

結論

要查詢子列表中的公共元素,應該瞭解子列表是如何分配的以及其基本結構。列表包含在方括號內由公共元素分隔的元素。解釋了使用模組、遞迴函式和簡單的迭代方法列印列表子列表中公共元素的方法。

更新於:2023-08-29

241 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.