使用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()将其转换为单个元素。然后使用函数返回获取新列表。现在在变量nested_list中创建展平列表,并将相同的变量作为参数用于调用函数以将其存储在变量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的函数,该函数接受名为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]
使用递归
程序使用递归函数来处理展平列表,并使用内置函数isinstance()检查对象的类型,从而帮助将展平列表生成单个元素。
示例
在下面的示例中,程序通过提取每个元素来迭代地展平构造的列表。然后,它打印包含嵌套列表中所有单个元素的展平列表。
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编程作业或任务,了解如何展平列表都将非常有用。
数据结构
网络
关系数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP