使用Python将列表展开为单独的元素

WBOY
WBOY 转载
2023-09-08 23:57:03 368浏览

使用Python将列表展开为单独的元素

列表是 Python 中一种强大的数据结构,可以保存许多不同类型的值。我们可能会遇到嵌套列表,其中项目本身就是列表。在这种情况下,扁平化列表并从层次结构中检索各个元素可能至关重要。在Python中,我们有一些内置函数 - chain()、extend()和append(),可用于解决将列表扁平化为单个元素的问题。

让我们举个例子:

输入

# Flatten List
my_list = [[11, 21, 31], [20, 30, 40], [4, 5, 6], [1, 2, 3]]

输出

# individual element in the list
[11, 21, 31, 20, 30, 40, 4, 5, 6, 1, 2, 3]

语法

所有示例均使用以下语法 -

itertools.chain()

itertools是提供chain()内置函数的模块名称。该函数将多个迭代器作为参数返回单个迭代器。

extend()

extend() 是 Python 中的内置方法,用于将特定列表元素添加到当前列表的末尾。

append()

append()是Python中的内置方法,可以将元素添加到列表末尾。

[[],[],[]]

上面的表示说明了嵌套列表结构。

使用嵌套循环

程序使用嵌套的 for 循环来迭代子列表,并使用append()方法帮助

示例

在以下示例中,通过定义名为 flatten_list 的函数来启动程序,该函数接受参数为 lst(以接收输入列表的值)。然后在变量 flattened 中创建空列表,该列表将存储包含各个元素的列表。接下来,使用嵌套的 for 循环迭代列表中的每个子列表,并使用append() 将子列表转换为单个元素。然后使用函数 return 来获取新列表。现在在变量nested_list中创建Flatten列表,并使用与调用函数中的参数相同的变量将其存储在变量f_list中。最后,我们在变量 f_list 的帮助下打印结果。

def flatten_list(lst):
    flattened = []
    for sublist in lst:
        for item in sublist:
            flattened.append(item)
    return flattened

# Create the flattened list
nested_list = [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
# Calling function used in the variable
f_list = flatten_list(nested_list)
print("The individual element in the list:\n", f_list)

输出

 The individual element in the list:
 [10, 20, 30, 40, 50, 60, 70, 80, 90]

使用列表理解

该程序使用列表理解,其中实现嵌套 for 循环以将 Flatten List 转换为单个元素。

示例

在以下示例中,通过定义名为 flatten_list 的函数开始程序,该函数接受名为 lst 的参数,该参数存储输入列表的值。然后使用列表理解返回函数,其中嵌套的 for 循环迭代到子列表中。接下来,创建展平列表并将其存储在变量 n_list 中。然后在变量 f_list 中使用调用函数,并在 print 函数中使用相同的变量来获取结果。

def flatten_list(lst):
    return [item for sublist in lst for item in sublist]

# Create the flatten list
n_list = [[11, 21, 31], [41, 51, 61], [71, 81, 91]]
# calling function used in the variable
f_list = flatten_list(n_list)
print("The individual element in the list:\n", f_list)

输出

 The individual element in the list:
 [11, 21, 31, 41, 51, 61, 71, 81, 91]

使用 itertools.chain() 函数

这是一个使用 itertools 模块来展平嵌套列表的 Python 程序。 flatten_list 函数采用嵌套列表作为参数,并返回一个包含子列表中所有元素的新列表。 itertools.chain 函数将所有子列表组合成一个可迭代对象,然后使用 list 函数将其转换为列表。然后,程序创建一个名为nested_list的嵌套列表,使用该列表作为参数调用flatten_list函数,并将结果分配给变量flattened_list。

示例

在下面的示例中,通过导入名为 itertools 的模块来开始程序,这将有助于将扁平列表转换为单独的元素。然后开始创建接受参数lst的函数flatten_list

import itertools
def flatten_list(lst):
    return list(itertools.chain(*lst))
# Create the flatten list
nested_list = [[12, 22, 32], [42, 52, 62], [72, 82, 92]]
# Calling function used in the variable
flattened_list = flatten_list(nested_list)
print("The individual element in the list:\n", flattened_list)

输出

 The individual element in the list:
 [12, 22, 32, 42, 52, 62, 72, 82, 92]

使用递归

该程序使用递归函数来处理展平列表,并使用内置函数 isintance() 它将检查对象类型的条件,并帮助生成单个元素的展平列表。

示例

在下面的示例中,程序通过提取每个元素来迭代地展平构造的列表。然后,它打印包含嵌套列表中所有单个元素的展平列表。

def flatten_list(lst):
    emp_list = []
    for item in lst:
        if isinstance(item, list):
            emp_list.extend(flatten_list(item))
        else:
            emp_list.append(item)
    return emp_list


# Create the flatten list
nested_list = [[81, 82, 83], [84, 85, 86], [87, 88, 89, 99, 100]]
f_list = flatten_list(nested_list)
print("The individual element in the list:\n", f_list)

输出

 The individual element in the list:
 [81, 82, 83, 84, 85, 86, 87, 88, 89, 99, 100]

结论

我们讨论了解决问题陈述的各种方法。我们了解扁平化列表将如何为我们提供一项基本技能,使我们能够更有效地处理复杂的数据结构并访问单个元素。此外,无论我们是从事数据处理、算法问题解决还是任何其他 Python 编程作业或任务,了解如何展平列表都会很有用。

以上就是使用Python将列表展开为单独的元素的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除