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
结论
掌握将三维列表转换为二维列表的不同方法对于成为一名高效的程序员非常重要。可以选择上述任何一种方法,具体取决于方便程度和程序的应用领域。
广告