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的函数,该函数接受名为lst的参数,该参数存储输入列表的值。然后使用列表生成式返回函数,其中嵌套的for循环迭代进入子列表。接下来,创建扁平化列表并将其存储在变量n_list中。然后使用变量f_list调用函数,并将同一变量用于打印函数以获取结果。
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编程作业或任务时都会很有用。