Python中將三維列表轉換為二維列表的方法


Python是一種廣泛使用的程式語言,用於各種目的,例如Web開發、資料科學、機器學習以及執行不同的自動化操作。在本文中,我們將學習將三維列表轉換為二維列表的不同方法。

讓我們首先看看這兩種型別的列表是什麼樣的:

三維列表

Three_dimensional_list = [
   [['Sam', 'Tom'], ['Miller', 'David']],
   [['Rocky', 'Tyler'], ['Mason', 'Petrick']],
   [['Stefen', 'Damon'], ['Matt', 'Jeremy']]
] # This is the basic structure of a three dimensional list

二維列表

Two_dimensional_list = [
   ['Sam', 'Tom'],
   ['Miller', 'David'],
   ['Rocky', 'Tyler'],
   ['Mason', 'Petrick'],
   ['Stefen', 'Damon'],
   ['Matt', 'Jeremy']
] # This is the basic structure of a two Dimensional list

將三維列表轉換為二維列表的不同方法

巢狀迴圈

這是將三維列表轉換為二維列表最快的方法。我們將簡單地檢查列表中的每個元素,並將其展平成二維列表。讓我們來看一個例子,以便更好地理解:

示例

def convert_list_to_2D(main_list): # The 3 dimensional list is given as input to the function convert_list_to_2D
   final_list = [] # An empty list is created to store all the 2 dimension list elements
   for sublist in main_list: # For loop is used to check each element in the sublists in the 3D list 
      for subsublist in sublist: # Second for loop to chek each element in the sublists of sublists of the 3D list
         final_list.append(subsublist) # All the sublists are moved to the final new list created
   return final_list

Three_dimensional_list = [
   [['Sam', 'Tom'], ['Miller', 'David']],
   [['Rocky', 'Tyler'], ['Mason', 'Petrick']],
   [['Stefen', 'Damon'], ['Matt', 'Jeremy']]
] # The 3D list is given as input

Two_dimensional_list = convert_list_to_2D(Three_dimensional_list) # The function convert_list_to_2D is run
print(Two_dimensional_list)

輸出

上述示例的輸出如下:

[['Sam', 'Tom'], ['Miller', 'David'], ['Rocky', 'Tyler'], ['Mason', 
'Petrick'], ['Stefen', 'Damon'], ['Matt', 'Jeremy']] # The 3D list is 
converted into 2D list

列表推導式

這是一種非常有效的方法,可以檢查給定輸入列表中存在的每個元素。讓我們來看一個例子,以便更好地理解:

示例

def convert_list_to_2D(main_list): # The 3 dimensional list is given as input to the function convert_list_to_2D
   return [subsublist for sublist in main_list for subsublist in sublist] # We will use for loop to check each element in sublist and sublist of sublist and convert them into a 2D list

Three_dimensional_list = [
   [['Sam', 'Tom'], ['Miller', 'David']],
   [['Rocky', 'Tyler'], ['Mason', 'Petrick']],
   [['Stefen', 'Damon'], ['Matt', 'Jeremy']]
] # The 3D list is given as input

Two_dimensional_list = convert_list_to_2D(Three_dimensional_list) # The function convert_list_to_2D is run
print(Two_dimensional_list)

# Note: This method is more preferable because it is a compact method and less lines of codes are required

輸出

上述示例的輸出如下:

[['Sam', 'Tom'], ['Miller', 'David'], ['Rocky', 'Tyler'], ['Mason', 
'Petrick'], ['Stefen', 'Damon'], ['Matt', 'Jeremy']] # The 3D list is 
converted into 2D list

NumPy庫

當列表中的輸入資料為數值形式時,此方法最為有用。讓我們來看一個例子,以便更好地理解:

示例

import numpy as np # Do not forget to import numpy or else error might occur
def convert_list_to_2D(main_list): # The 3 dimensional list is given as input to the function convert_list_to_2D
   flattened = np.array(main_list).reshape(-1, len(main_list[0][0])) # With the help of np.array we will convert the input list into an array and with the help of reshape, we reshape the array into 2D form
   return flattened.tolist() # The 2D form of array will be converted back into list with the help of tolist function

Three_dimensional_list = [
   [[14, 28], [33, 47]],
   [[59, 36], [71, 18]],
   [[96, 13], [81, 32]]
] # The list is given as input

Two_dimensional_list = convert_list_to_2D(Three_dimensional_list) # The function convert_list_to_2D is run
print(Two_dimensional_list)

輸出

上述示例的輸出如下:

[[14, 28], [33, 47], [59, 36], [71, 18], [96, 13], [81, 32]] # The 3D list is 
converted into 2D list

手動迭代

在此方法中,我們將手動檢查給定輸入列表中的每個元素。讓我們來看一個例子,以便更好地理解:

示例

def convert_list_to_2D(main_list): # The 3 dimensional list is given as input to the function convert_list_to_2D
# We will first calculate the dimensions of the 2D List
   rows = len(main_list) * len(main_list[0]) # # The number of rows in 2D list will be decide by product of number of sublist in 3D list with the number of sublists in each sublists
   cols = len(main_list[0][0])
   output_list = [[0 for _ in range(cols)] for _ in range(rows)] # A new list to store elements is created and for loop is used to check each element in the list given as input
   
   for i in range(len(main_list)):
      for j in range(len(main_list[i])):
         for k in range(len(main_list[i][j])):
            output_list[i * len(main_list[i]) + j][k] = main_list[i][j][k] 
# Proper values are assigned different positions in the new list created
   
   return output_list

Three_dimensional_list = [
   [['Sam', 'Tom'], ['Miller', 'David']],
   [['Rocky', 'Tyler'], ['Mason', 'Petrick']],
   [['Stefen', 'Damon'], ['Matt', 'Jeremy']]
] # The 3D list is given as input

Two_dimensional_list = convert_list_to_2D(Three_dimensional_list) # The function convert_list_to_2D is run
print(Two_dimensional_list)

輸出

上述示例的輸出如下:

[['Sam', 'Tom'], ['Miller', 'David'], ['Rocky', 'Tyler'], ['Mason', 
'Petrick'], ['Stefen', 'Damon'], ['Matt', 'Jeremy']] # The 3D list is 
converted into 2D list

結論

掌握將三維列表轉換為二維列表的不同方法對於成為一名高效的程式設計師非常重要。可以選擇上述任何一種方法,具體取決於方便程度和程式的應用領域。

更新於:2023年8月7日

270 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告