Python程式連線跨列表的每個元素


在本文中,我們將學習一個 Python 程式來連線跨列表的每個元素。

使用的方法

以下是完成此任務的各種方法 -

  • 使用列表推導式和字串連線

  • 使用 itertools.product() 函式

示例

假設我們已經獲取了兩個輸入列表。我們現在將連線這兩個給定列表中的每個元素

輸入

inputList1 = ["hello", "tutorialspoint", "python"]
inputList2 = ["coding", "articles"]

輸出

Resultant paired combinations list:
['hello coding', 'hello articles', 'tutorialspoint coding', 'tutorialspoint articles', 'python coding', 'python articles']

方法 1:使用列表推導式和字串連線

列表推導式

當您希望基於現有列表的值構建新列表時,列表推導式提供了一種更短/簡潔的語法。

演算法(步驟)

以下是執行所需任務需要遵循的演算法/步驟 -。

  • 建立一個變數來儲存**輸入**列表 1 並列印給定的第一個列表。

  • 建立另一個變數來儲存**輸入**列表 2 並列印給定的第二個列表。

  • 使用列表推導式遍歷第一個列表和第二個列表(巢狀迴圈)。

  • 在其中,使用來自第一個列表的第一個元素和來自第二個列表的第二個元素建立元組對。

  • 透過迭代對列表,使用字串連線連線對元素。

  • 列印結果配對組合列表

示例

以下程式使用列表推導式和字串連線返回跨列表每個元素的連線 -

# input list 1
inputList1 = ["hello", "tutorialspoint", "python"]

# input list 2
inputList2 = ["coding", "articles"]

# printing input list 1
print("Input List 1:", inputList1)

# printing input list 2
print("Input List 2:", inputList2)

# Using a list to traverse the first list and the second list

# Creating tuple pairs with the first element from the first list

# and the second element from the second list
pairs_list = [(p, q) for p in inputList1 for q in inputList2]

# Iterating in the above pairs list and

# concatenating the tuples(string concatenation)
resultantList = [m + ' ' + n for (m, n) in pairs_list]

# printing resultant paired combinations list
print("Resultant paired combinations list:\n", resultantList)

輸出

執行後,上述程式將生成以下輸出 -

Input List 1: ['hello', 'tutorialspoint', 'python']
Input List 2: ['coding', 'articles']
Resultant paired combinations list:
['hello coding', 'hello articles', 'tutorialspoint coding', 'tutorialspoint articles', 'python coding', 'python articles']

**時間複雜度** - O(n^2),因為有兩個 for 迴圈

**空間複雜度** - O(n)

方法 2:使用 itertools.product() 函式

**Itertools.product()** 返回笛卡爾積。

讓我們看看什麼是**笛卡爾積** -

在數學方面,所有有序對 (a, b) 的集合(其中 a 屬於 A,b 屬於 B)被稱為兩個集合的笛卡爾積。請檢視下面的示例以更好地理解。

示例

list 1= [10, 20, 30]
list2 = [2, 4, 6]

輸出

[(10, 2), (10, 4), (10, 6), (20, 2), (20, 4), (20, 6), (30, 2), (30, 4), (30, 6)]

演算法(步驟)

以下是執行所需任務需要遵循的演算法/步驟 -

  • 使用 import 關鍵字從 itertools 模組匯入**product()** 函式。

  • 建立一個變數來儲存**輸入**列表 1 並列印給定的第一個列表。

  • 建立另一個變數來儲存**輸入**列表 2 並列印給定的第二個列表。

  • 獲取一個空列表來儲存結果。

  • 使用 product() 函式遍歷給定列表的笛卡爾積。

  • 使用 + 運算子(字串連線)連線兩個列表元素。

  • 使用 append() 函式將此對字串新增到結果中。

  • 列印結果。

示例

以下程式使用 itertools.product() 和字串連線返回跨列表每個元素的連線 -

# importing product from itertools module
from itertools import product

# input list 1
inputList1 = ["hello", "tutorialspoint", "python"]

# input list 2
inputList2 = ["coding", "articles"]

# printing input list 1
print("Input List 1:", inputList1)

# printing input list 2
print("Input List 2:", inputList2)

# Taking an empty list to store the concatenation result
resultantList = []

# Iterating the cartesian product of the two lists
for e in product(inputList1, inputList2):
   
   # Concatenating two lists elements with space as a separator
   pairString = e[0]+' '+e[1]

   # Adding this pair string to the result list at the end of the result list
   resultantList.append(pairString)

# printing resultant paired combinations list
print("Resultant paired combinations list:\n", resultantList)

輸出

執行後,上述程式將生成以下輸出 -

Input List 1: ['hello', 'tutorialspoint', 'python']
Input List 2: ['coding', 'articles']
Resultant paired combinations list:
['hello coding', 'hello articles', 'tutorialspoint coding', 'tutorialspoint articles', 'python coding', 'python articles']

結論

在本文中,我們學習瞭如何使用兩種不同的方法連線給定的兩個列表中的每個元素。此外,我們瞭解了笛卡爾積是什麼,如何使用 product() 方法計算它以及示例。

更新於: 2023年1月27日

513 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.