列表理解如何平化python列表? [英] How does the list comprehension to flatten a python list work?

查看:71
本文介绍了列表理解如何平化python列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近正在寻找一种将嵌套的python列表扁平化的方法,例如:[[1,2,3],[4,5,6]]变为:[1,2,3,4,5 ,6].

I recently looked for a way to flatten a nested python list, like this: [[1,2,3],[4,5,6]], into this: [1,2,3,4,5,6].

Stackoverflow一如既往地很有帮助,我发现具有巧妙的列表理解功能的帖子:

Stackoverflow was helpful as ever and I found a post with this ingenious list comprehension:

l = [[1,2,3],[4,5,6]]
flattened_l = [item for sublist in l for item in sublist]

我以为我了解列表推导的工作原理,但是显然我没有最模糊的想法.最让我困惑的是,除了上面的理解之外,这还可以运行(尽管不会给出相同的结果):

I thought I understood how list comprehensions work, but apparently I haven't got the faintest idea. What puzzles me most is that besides the comprehension above, this also runs (although it doesn't give the same result):

exactly_the_same_as_l = [item for item in sublist for sublist in l]

有人可以解释python如何解释这些东西吗?基于第二种理解,我希望python能够将其解释回前端,但显然并非总是如此.如果是这样,则第一次理解将引发错误,因为子列表"不存在.我的思想完全扭曲了,救命!

Can someone explain how python interprets these things? Based on the second comprension, I would expect that python interprets it back to front, but apparently that is not always the case. If it were, the first comprehension should throw an error, because 'sublist' does not exist. My mind is completely warped, help!

推荐答案

然后让我们看一下列表理解,但是首先让我们从最简单的列表理解入手.

Let's take a look at your list comprehension then, but first let's start with list comprehension at it's easiest.

l = [1,2,3,4,5]
print [x for x in l] # prints [1, 2, 3, 4, 5]

您可以将其视为与for循环一样的结构,如下所示:

You can look at this the same as a for loop structured like so:

for x in l:
    print x

现在让我们看看另一个:

Now let's look at another one:

l = [1,2,3,4,5]
a = [x for x in l if x % 2 == 0]
print a # prints [2,4]

与以下内容完全相同:

a = []
l = [1,2,3,4,5]
for x in l:
    if x % 2 == 0:
        a.append(x)
print a # prints [2,4]

现在让我们看一下您提供的示例.

Now let's take a look at the examples you provided.

l = [[1,2,3],[4,5,6]]
flattened_l = [item for sublist in l for item in sublist]
print flattened_l # prints [1,2,3,4,5,6]

要进行列表理解,请从最左边的for循环开始,然后逐步进行.在这种情况下,将添加变量item.它将产生等效的结果:

For list comprehension start at the farthest to the left for loop and work your way in. The variable, item, in this case, is what will be added. It will produce this equivalent:

l = [[1,2,3],[4,5,6]]
flattened_l = []
for sublist in l:
    for item in sublist:
        flattened_l.append(item)

现在是最后一个

exactly_the_same_as_l = [item for item in sublist for sublist in l]

使用相同的知识,我们可以创建一个for循环并查看其行为L

Using the same knowledge we can create a for loop and see how it would behaveL

for item in sublist:
    for sublist in l:
        exactly_the_same_as_l.append(item)

现在,上述方法起作用的唯一原因是,当创建flattened_l时,它也创建了sublist.这是为什么没有引发错误的范围界定的原因.如果您在没有先定义flattened_l的情况下运行它,则会得到NameError

Now the only reason the above one works is because when flattened_l was created, it also created sublist. It is a scoping reason to why that did not throw an error. If you ran that without defining the flattened_l first, you would get a NameError

这篇关于列表理解如何平化python列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆