Python - 從列表建立巢狀記錄列表


我們手頭的問題是,必須使用 Python 建立一個演算法,以便從給定的多個列表中獲取巢狀記錄列表。有時,出於實際應用中的某些原因,我們需要合併給定的列表。因此,解決此問題將有助於解決這些問題。

理解問題的邏輯

在此問題中,我們將獲得兩個或多個列表,並且必須透過應用邏輯來組合並形成巢狀記錄列表。因此,我們將使用不同的方法來完成此任務。

首先,我們將使用 zip 方法壓縮所有作為輸入給出的列表,zip 方法將生成單個巢狀列表。其次,我們將使用帶有 zip 方法的迴圈並建立一個新列表,該列表將包含給定輸入列表的所有項。第三,我們將使用使用者定義函式解決此問題。

第一種方法 - 演算法

在此方法中,我們將使用 Python 的 zip 方法,該方法將根據列表項的索引組合列表。

  • 步驟 1 - 初始化列表。在我們的程式碼中,我們將使用 Name、DOB 和 Birth_Place 列表。

  • 步驟 2 - 現在使用名為 combined_lists 的新列表,此列表將儲存從給定列表生成的巢狀記錄列表。並使用 zip 方法,我們將透過在 zip 方法中傳遞所有列表來建立此列表。

  • 步驟 3 - 列印 combined_lists 以顯示輸出。

示例

Name= ["John","Riya", "Amit"]

DOB = ["11-09-1998", "17-02-1993", "02-05-2003"]

Birth_Place = ["Delhi", "Mumbai", "Pune"]

combine_lists = zip(Name, DOB, Birth_Place)

print("The combined list for all the lists")

print(list(combine_lists))

輸出

The combined list for all the lists
[('John', '11-09-1998', 'Delhi'), ('Riya', '17-02-1993', 'Mumbai'), ('Amit', '02-05-2003', 'Pune')]

第二種方法 - 演算法

這是使用 for 迴圈與 zip 方法完成此任務的另一種方法。

  • 步驟 1 - 定義 list1 和 list 2,它們將用於組合以建立巢狀列表。

  • 步驟 2 - 使用 the_key 變數在新的列表中新增鍵,其值將為 id。

  • 步驟 3 - 現在,我們將建立一個名為 nested_list 的新列表,並透過為每個專案建立鍵來初始化它,然後使用 zip 方法在外部列表中再次建立巢狀列表。

  • 步驟 4 - 獲取 nested_list 後,我們將列印輸入和輸出列表的值。

示例

# Initialize the lists
list1 = ['Tea', 'Coffee']
list2 = [['Black', 'Green', 'White'], ['Espresso', 'Latte', 'Cappuccino']]

the_key = 'id'

nested_list = {key : [{the_key : index} for index in val]
   for key, val in zip(list1, list2)}

# Print the input lists
print("The first input list : " + str(list1))
print("The second input list : " + str(list2))

# Result print on the console
print("The constructed dictionary is : " + str(nested_list))

輸出

The first input list : ['Tea', 'Coffee']
The second input list : [['Black', 'Green', 'White'], ['Espresso', 'Latte', 'Cappuccino']]
The constructed dictionary is : {'Tea': [{'id': 'Black'}, {'id': 'Green'}, {'id': 'White'}], 'Coffee': [{'id': 'Espresso'}, {'id': 'Latte'}, {'id': 'Cappuccino'}]}

第三種方法 - 演算法

在此方法中,我們將定義一個函式,以使用多個輸入列表生成巢狀列表。

  • 步驟 1 - 首先定義名為 generate_nested_list 的函式,並將所有輸入列表作為引數傳遞給它。在這裡,我們將使用星號或星號運算子 (*) 透過單個帶有星號運算子的變數傳遞多個變數。

  • 步驟 2 - 現在定義一個空白列表作為 combined_list,它將儲存結果巢狀列表。

  • 步驟 3 - 現在,我們將使用 for 迴圈組合列表,並將所有專案追加到 combined_list 中。

  • 步驟 4 - 透過呼叫函式並向函式中傳遞所有列表來列印 combined_list。

示例

# Define the function to generate nested list
def generate_nested_list(*lists):
   combined_list = []
   for lst in lists:
      combined_list.append(lst)
   return combined_list

#Initialize the lists to generate nested list
list1 = ['I', 'am', 'using']
list2 = ['Tutorials', 'point', 'to']
list3 = ['learn', 'Python', 'Programming']
nested_list = generate_nested_list(list1, list2, list3)
print("The created nested list is as follows\n", nested_list)

輸出

The created nested list is as follows
 [['I', 'am', 'using'], ['Tutorials', 'point', 'to'], ['learn', 'Python', 'Programming']]

複雜度

從給定列表建立新的巢狀記錄列表的時間複雜度為 O(n),其中 n 是所有列表中存在的專案總數。因為我們對所有方法都執行了簡單的操作,這些操作需要線性時間才能完成任務。

結論

由於我們已經使用 Python 中的各種方法成功地解決了給定的問題。因此,在這裡我們學習瞭如何使用簡單的方法從給定的多個列表組合或建立巢狀列表。當我們需要從給定的多個數據集合並和建立一個新的資料集時,此任務可能會有所幫助。

更新於: 2023年10月17日

93 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.