介绍

列表被认为是Python编程语言中最灵活的数据结构之一。另一方面,二维列表或2D列表通常被称为列表的列表,是一个列表对象,其中每个元素本身都是一个列表。例如:[[19, 18, 17], [16, 15, 14], [13, 12, 11]]。

将列表的列表展开是将二维列表转换为一维列表的过程,方法是将列表的列表中保存的每个列表元素解嵌套,即将[[9, 8, 7], [6, 5, 4], [3, 2, 1]]转换为[9, 8, 7, 6, 5, 4, 3, 2, 1]。

我们可以使用嵌套的 for 循环、递归、列表理解、核心函数或导入Python中的库或包来执行展开过程,具体取决于嵌套列表的深度和规则。

在本教程中,我们将研究使用Python编程语言展开嵌套列表的各种方法。但在开始之前,让我们了解嵌套列表的类型。

嵌套列表有哪些类型?

正如我们所知,Python是一种弱类型编程语言。因此,我们可能会遇到两种列表的列表。这些列表的列表或嵌套列表如下:

  1. 常规的列表列表
  2. 不规则的列表列表

常规的列表列表

常规的列表列表中的每个项目都被称为子列表,因此观察元素类型的统一性。例如:[[9, 8, 7], [6, 5, 4], [3, 2, 1]]是一个常规的列表列表,因为[9, 8, 7],[6, 5, 4],[3, 2, 1]都是列表类型。

不规则的列表列表

不规则的列表列表中的每个项目要么被称为子列表,要么被称为非列表元素(例如字符串或整数)。因此,元素类型方面存在不规则性。例如:[[9, 8, 7], [6, 5], 4, 3]是不规则的列表列表,因为[9, 8, 7]和[6, 5]都是列表类型,而4和3是整数类型。

使用嵌套for循环展开列表的列表

使用嵌套 for 循环展开列表的列表被认为是一种蛮力方法来获得一个平面列表。我们可以通过选择二维列表中的每个项目并将其排列在一维列表中来执行此方法。

让我们考虑以下示例,该示例适用于常规和不规则的列表列表。

示例:

# defining the function  
def flattenlist(_2dlist):  
    # defining an empty list  
    flatlist = []  
    # Iterating through the outer list  
    for item in _2dlist:  
        if type(item) is list:  
            # If the item is of the list type, iterating through the sub-list  
            for element in item:  
                flatlist.append(element)  
        else:  
            flatlist.append(item)  
    return flatlist  
  
# defining the nested list  
nestedlist = [[10, 20, 30, 40], [50, 60, 70], [80, 90, 100]]  
print('Genuine List:', nestedlist)  
print('Converted Flat List:', flattenlist(nestedlist))  

输出:

Genuine List: [[10, 20, 30, 40], [50, 60, 70], [80, 90, 100]]
Converted Flat List: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

说明:

在上面的示例中,我们定义了一个名为 flattenlist 的函数,该函数以 _2dlist 作为参数。然后,我们使用for循环遍历嵌套列表的元素并将它们附加到生成的平面列表中。接下来,我们定义了嵌套列表并实现了 flattenlist 函数。结果,嵌套列表已成功转换为平面列表。

使用列表理解展开嵌套列表

使用列表理解来展开列表的列表是一种优雅的方法,取决于现有的二维列表。但是,这种方法提供的解决方案不太直观。

让我们考虑以下示例。

示例:

# defining the nested list  
nestedlist = [[10, 20, 30, 40], [50, 60, 70], [80, 90]]  
# list comprehension  
flatlist = [element for sub_list in nestedlist for element in sub_list]  
print('Genuine list:', nestedlist)  
print('Converted list:', flatlist)  

输出:

Genuine list: [[10, 20, 30, 40], [50, 60, 70], [80, 90]]
Converted list: [10, 20, 30, 40, 50, 60, 70, 80, 90]

说明:

在上面的示例中,我们定义了一个嵌套列表并使用列表理解。然后,我们将它们打印给用户。结果,嵌套列表已成功转换为平面列表。

使用递归方法展开列表的列表

我们还可以使用递归方法展开二维列表。让我们考虑以下示例,该示例实现了递归方法以展开列表的列表。这种实现对于常规和不规则的列表列表都有效。

示例:

# defining a function  
def flattenlist(nestedlist):  
    if len(nestedlist) == 0:  
        return nestedlist  
    if isinstance(nestedlist[0], list):  
        return flattenlist(nestedlist[0]) + flattenlist(nestedlist[1:])  
    return nestedlist[:1] + flattenlist(nestedlist[1:])  
  
print(flattenlist([[10, 20, 30, 40], [50, 60, 70], [80, 90], 100]))  

输出:

[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

说明:

在上面的示例中,我们定义了一个函数并使用if语句来判断嵌套列表的长度是否为零,如果是,则返回嵌套列表。如果零号索引上的数据元素是列表的实例,则列表索引再次进入函数并添加到列表的后续索引中,依此类推。否则,函数将返回元素以及后续元素。最后,我们定义了嵌套列表并执行了函数。结果,列表的列表已成功递归展开。

利用Python库

我们还可以利用一些Python编程语言库来展开列表的列表。下面描述了这些库的实现方式。

使用functools和operator库展开列表的列表

operator库提供了iconcat()函数,用于执行基本操作,如连接。我们可以将此函数累积应用于嵌套列表的数据元素,从左到右,从而将嵌套列表减少为平面列表。

让我们考虑以下示例以了解其实现。

示例:

# importing the required libraries  
import operator  
import functools  
regularlist = []  
  
# Converting the list of lists into a flattened one.  
def convo(nestedlist):  
    for element in nestedlist:  
        if type(element) is list:  
            regularlist.append(element)  
        else:  
            regularlist.append([element])  
    return regularlist  
  
twoDlist = [[10, 20, 30, 40], [50, 60, 70], [80, 90, 100], 110]  
regular2Dlist = convo(twoDlist)  
print('Given List:', twoDlist)  
print('Converted list:', functools.reduce(operator.iconcat, regular2Dlist, []))  

输出:

Given List: [[10, 20, 30, 40], [50, 60, 70], [80, 90, 100], 110]
Converted list: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110]

说明:

在上面的示例中,我们导入了 functools 库和 operator 库,并生成了一个嵌套列表。然后,我们定义了一个名为 convo 的函数,用于将列表的列表转换为平面列表。在此函数中,我们使用 for 循环,其中从嵌套列表中附加了元素到我们之前定义的空列表中。然后我们定义了嵌套列表并执行了函数。结果,嵌套列表已成功转换为平面列表。

使用itertools库展开列表的列表

itertools库提供了chain()函数,允许我们将嵌套列表理想地转换为单个平面列表。此函数通过按顺序通过传递的可迭代对象来迭代它们,将连续的系列视为单个系列。

让我们考虑以下示例:

示例:

# importing the itertools library  
import itertools  
  
# defining the nested list  
nestedlist = [[10, 20, 30, 40], [50, 60, 70], [80, 90, 100]]  
flattenlist = list(itertools.chain(*nestedlist))  
  
print('The nested list:', nestedlist)  
print('The flattened list:', flattenlist 

输出:

The nested list: [[10, 20, 30, 40], [50, 60, 70], [80, 90, 100]]
The flattened list: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

说明:

在上面的示例中,我们导入了 itertools 库并生成了一个嵌套列表。然后,我们使用 chain() 函数将给定的嵌套列表转换为平面列表。最后,我们将结果返回给用户。因此,嵌套列表已成功展开。

使用NumPy库展开列表的列表

NumPy库提供各种日常操作,包括按列或按行连接二维常规数组。我们将使用名为 flat 的属性来获得数组上的一维迭代器以实现目标。让我们考虑以下示例,了解 concatenate 函数和 flat 属性的用法。

示例:

# importing the library  
import numpy  
  
# defining the nested list  
nestedlist = [[10, 20, 30, 40], [50, 60, 70], [80, 90]]  
  
# using the concatenate function along with the flat attribute  
flattenlist = list(numpy.concatenate(nestedlist).flat)  
  
print('The Nested list:', nestedlist)  
print('The Flattened list:', flattenlist)

输出:

The Nested list: [[10, 20, 30, 40], [50, 60, 70], [80, 90]]
The Flattened list: [10, 20, 30, 40, 50, 60, 70, 80, 90]

说明:

在上面的示例中,我们导入了 numpy 库并定义了一个嵌套列表。然后,我们使用 numpy 库的 concatenate 函数以及其 flat 属性来展开嵌套列表的元素并将它们连接到新的展平列表中。最后,我们将结果打印给用户。因此,嵌套列表已成功展开。

利用核心函数

我们还可以利用Python编程语言提供的一些核心函数来执行展开任务。

使用sum函数展开列表的列表

将内部列表求和作为解决问题的另一种方法。我们将两个参数传递给 sum 函数:第一个参数是 iterable,即嵌套列表,第二个参数是 start,即以下情况的空列表,作为初始平面列表,在其中添加内部子列表的数据元素。

我们可以说,这种方法非常方便,因为我们不必导入任何东西。但是,当嵌套列表中存在大量子列表时,它比 itertools()chain() 函数慢。

让我们考虑以下示例:

示例:

# defining a nested list  
nestedlist = [[10, 20, 30, 40], [50, 60, 70], [80, 90]]  
  
# using the sum function  
flattenlist = sum(nestedlist, [])  
  
print('The Nested list:', nestedlist)  
print('The Flattened list:', flattenlist)  

输出:

The Nested list: [[10, 20, 30, 40], [50, 60, 70], [80, 90]]
The Flattened list: [10, 20, 30, 40, 50, 60, 70, 80, 90]

说明:

在上面的示例中,我们定义了嵌套列表。然后,我们使用 sum() 函数将嵌套列表转换为一维列表,并将其打印给用户。因此,嵌套列表已成功展开。

使用Lambda关键字展开列表的列表

我们可以使用关键字 lambda 定义匿名函数。我们可以将常规/不规则列表作为参数传递给这个匿名函数。对表达式进行评估以获得平面的一维列表。

让我们考虑以下示例:

示例:

# Defining the nested list  
nestedlist = [[10, 20, 30], [30, 50, 60], [40, 60, 70], 70]  
  
# Using lambda parameters: expression  
flattenlist = lambda nestedlist:[item for element in nestedlist for item in flattenlist(element)] if type(nestedlist) is list else [nestedlist]  
  
print("The Nested list:", nestedlist)  
print("The Flattened List:", flattenlist(nestedlist))  

输出:

The Nested list: [[10, 20, 30], [30, 50, 60], [40, 60, 70], 70]
The Flattened List: [10, 20, 30, 30, 50, 60, 40, 60, 70, 70]

说明:

在上面的示例中,我们定义了一个嵌套列表。然后,我们使用 lambda关键字以及一个定义列表理解的参数来定义匿名函数。然后我们将它们打印给用户。因此,我们已成功将二维不规则列表转换为平面列表。

标签: Tkinter教程, Tkinter安装, Tkinter库, Tkinter入门, Tkinter学习, Tkinter入门教程, Tkinter, Tkinter进阶, Tkinter指南, Tkinter学习指南, Tkinter进阶教程, Tkinter编程