Python 将一个3D列表转换为2D列表的方法

Python 将一个3D列表转换为2D列表的方法

Python是一种广泛使用的编程语言,用于不同的目的,如Web开发、数据科学、机器学习以及执行各种自动化操作。在本文中,我们将学习将3D列表转换为2D列表的不同方法。

首先让我们看看这两种类型的列表是什么样子:-

3D列表

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

2D列表

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

将3D列表转换为2D列表的不同方法

嵌套循环

这是将3D列表转换为2D列表的最快方法。我们只需检查列表中的每个元素,并将其展平为2D列表。让我们看一个示例,以更好地理解它:

示例

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

结论

对于成为高效的程序员来说,了解将3D列表转换为2D列表的不同方法非常重要。可以根据方便和程序应用领域的不同选择上述任何方法。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程