如何使用Python中的列表推导将多个列表展平

huangapple go评论70阅读模式
英文:

How to flatten multiple lists with list comprehension in python

问题

我目前有多个包含内部列表的列表。我已经找到了如何使用列表推导来展平这些列表,但如何在不重复使用相同的代码行的情况下做到呢?

以下是示例代码:

first = [[1,2,3], [4,5,6], [7,8,9]]
second = [[3,5,6], [0,3,4]]
third = [[2,5,0], [1,2,9]]

flat_first = [item for sublist in first for item in sublist]
flat_second = [item for sublist in second for item in sublist]
flat_third = [item for sublist in third for item in sublist]

这是我尝试过的:

lists = [first, second, third]
for i in lists:
   flattened = [item for sublist in i for item in sublist]

如果你希望避免重复相同的代码行,你可以将这个操作封装成一个函数,并在需要时调用该函数。这样可以提高代码的可维护性和重用性。

英文:

I currently have multiple lists that consists of inner lists. I've found out how to use list comprehension to flatten the lists but how can i do it without reusing the same line of code?

Here's an example code:

first = [[1,2,3], [4,5,6], [7,8,9]]
second = [[3,5,6], [0,3,4]]
third = [[2,5,0], [1,2,9]]

flat_first = [item for sublist in first for item in sublist]
flat_second = [item for sublist in second for item in sublist]
flat_third = [item for sublist in third for item in sublist]

Here is what i tried:

lists = [first, second, third]
for i in lists:
   flattened = [item for sublist in i for item in sublist]

答案1

得分: 1

通常情况下,如果你不想“重复使用”相同的代码行,你可以将它放入一个函数中。假设你想要“更新”变量 firstsecondthird,你不能在不重复变量名称的情况下这样做(并且重复 某些东西,就像你在你的第一个示例中所做的那样)。在这种情况下,你是否更喜欢使用字典?另外,itertools.chain.from_iterable 可能是最快的:

import itertools

my_lists = {
    'first': [[1,2,3], [4,5,6], [7,8,9]],
    'second': [[3,5,6], [0,3,4]],
    'third': [[2,5,0], [1,2,9]],
}

# 如果你想要一个简单的调用,可以将这段代码放入一个 update() 函数中!
for key, val in my_lists.items():
    my_lists[key] = list(itertools.chain.from_iterable(val))

这将更新原始字典:

>>> my_lists
{'first': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'second': [3, 5, 6, 0, 3, 4], 'third': [2, 5, 0, 1, 2, 9]}

否则,如果你只想使用列表推导来创建新的变量 flat_firstflat_secondflat_third,你已经在你的原始帖子中完成了,不过如果你想要一个一行的代码来完成:

flat_first, flat_second, flat_third = (list(itertools.chain.from_iterable(val)) for val in [first, second, third])

与 Ignatius 询问的类似,你的目标(期望的输出)是什么?

lists = [first, second, third]
flattened = [[item for sublist in some_list for item in sublist] for some_list in lists]

也是正确的,正如 Ignatius 所评论的那样,它会给你:

[[1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 5, 6, 0, 3, 4], [2, 5, 0, 1, 2, 9]]

不过,你正在创建_新的_值,我不确定这是否是你想要的。除非是这样。

英文:

Typically, if you don't want to "reuse" the same line of code, you would put it into a function. Assuming you want to "update" the variables first, second, and third, you cannot do so without repeating the variable name (and repeating something, like you have in your first example). In this case, might you prefer to use a dictionary? Also, itertools.chain.from_iterable is probably fastest:

import itertools

my_lists = {
    'first': [[1,2,3], [4,5,6], [7,8,9]],
    'second': [[3,5,6], [0,3,4]],
    'third': [[2,5,0], [1,2,9]],
}

# Put this into an update() function if you want a simple call!
for key, val in my_lists.items():
    my_lists[key] = list(itertools.chain.from_iterable(val))

Which updates the original dictionary:

>>> my_lists
{'first': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'second': [3, 5, 6, 0, 3, 4], 'third': [2, 5, 0, 1, 2, 9]}

Otherwise, if you simply want to use a list comprehension to create new variables flat_first, flat_second, and flat_third, you've already done that in your OP, although if you want a one-liner to do that:

flat_first, flat_second, flat_third = (list(itertools.chain.from_iterable(val)) for val in [first, second, third])

Similar to what Ignatius asked, what exactly is your goal (intended output) here?

lists = [first, second, third]
flattened = [[item for sublist in some_list for item in sublist] for some_list in lists]

Could also be correct, as commented by Ignatius, which gives you:

[[1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 5, 6, 0, 3, 4], [2, 5, 0, 1, 2, 9]]

Although you are creating new values, which I am not entirely sure is what you want. Unless it is.

答案2

得分: 1

我不确定您想要哪种解决方案,所以我提出了两个选项:

创建一个嵌套列表

first = [[1,2,3], [4,5,6], [7,8,9]]
second = [[3,5,6], [0,3,4]]
third = [[2,5,0], [1,2,9]]
lists = [first, second, third]

选项1(一个扁平化的列表)

flattened1 = [item for sublist in lists for subsublist in sublist for item in subsublist]
print(flattened1)

输出:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 3, 5, 6, 0, 3, 4, 2, 5, 0, 1, 2, 9]

选项2(一个包含扁平化列表的列表)

flattened2 = [[item for subsublist in sublist for item in subsublist] for sublist in lists]
print(flattened2)

输出:

[[1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 5, 6, 0, 3, 4], [2, 5, 0, 1, 2, 9]]

我还检查了您的代码,发现每次迭代都订阅了flattened变量,修复它的一个选项是:

flattened = []
for sublist in lists:
flattened.append([item for subsublist in sublist for item in subsublist])

print(flattened)

输出:

[[1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 5, 6, 0, 3, 4], [2, 5, 0, 1, 2, 9]]

如果其中一个选项对您有帮助,请告诉我,祝您愉快!

英文:

I'm not 100% sure witch solution you want, so I came up with two options:

# Making a list of lists
first = [[1,2,3], [4,5,6], [7,8,9]]
second = [[3,5,6], [0,3,4]]
third = [[2,5,0], [1,2,9]]
lists = [first, second, third]

# Option 1 (a single flattened list)

flattened1 = [item for sublist in lists for subsublist in sublist for item in subsublist]
print(flattened1)
# Output:
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 3, 5, 6, 0, 3, 4, 2, 5, 0, 1, 2, 9]

# Option 2 (a list of flattened lists)

flattened2 = [[item for subsublist in sublist for item in subsublist] for sublist in lists]
print(flattened2)
# Output:
# [[1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 5, 6, 0, 3, 4], [2, 5, 0, 1, 2, 9]]

I checked your code too, flattened is being subscribed each interaction, an option to fix it would be:

flattened = []
for sublist in lists:
    flattened.append([item for subsublist in sublist for item in subsublist])

print(flattened)
# Output:
# [[1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 5, 6, 0, 3, 4], [2, 5, 0, 1, 2, 9]]

Let me know if one of these options do help you,
have fun!

huangapple
  • 本文由 发表于 2023年5月28日 11:17:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定