[英]List inside of List Comprehension
So, I start out with this code: 所以,我从这段代码开始:
x = [1, [2, 3]]
y = [4, [5, 6]]
z = [7, [8, 9]]
amazing_list = [x, y, z]
and i do a simple list comprehension on it 我对它做了一个简单的列表理解
print [l for l in amazing_list]
prints [[1, [2, 3]], [4, [5, 6]], [7, [8, 9]]]
as expected 按预期打印
[[1, [2, 3]], [4, [5, 6]], [7, [8, 9]]]
print [l for i, l in amazing_list]
prints [[2, 3], [5, 6], [8, 9]]
as expected 按预期打印
[[2, 3], [5, 6], [8, 9]]
print [a for i, l in amazing_list for a in l]
prints [2, 3, 5, 6, 8, 9]
as expected, 按预期打印
[2, 3, 5, 6, 8, 9]
,
but then here's the kicker, here's where something isn't expected: what I'm trying to accomplish with the next line of code that I'm about to present is I'm just trying to print 2, 5, 8 (the first element of the list inside of the list): 但接下来就是踢球者了,这里有些东西没有被预料到:我要用下一行代码完成的目的是我要做的就是我只想尝试打印2,5,8(第一个列表内部列表的元素):
print [a for i, l in amazing_list for a, b in l]
but this prints TypeError: 'int' object is not iterable
. 但这会打印
TypeError: 'int' object is not iterable
。 Why? 为什么?
I know that these accomplish what I want to accomplish: 我知道这些完成了我想要完成的任务:
print [l[0] for i, l in amazing_list]
print [a for a, b in [l for i, l in amazing_list]]
But what I want to find is why, conceptually, my first attempt at the comprehension didn't work. 但我想要找到的是,从概念上讲,我在理解中的第一次尝试是行不通的。
You're getting a little carried away with the unpacking. 打开包装后你会感到有些不知所措。
Note that the comprehension you're trying to understand 请注意您正在尝试理解的理解
[a for i, l in amazing_list for a, b in l]
is logically equivalent to the following: 在逻辑上等同于以下内容:
tmp = []
for i, l in amazing_list:
for a, b in l:
tmp.append(a)
with the result being in tmp
. 结果在
tmp
。
Let's look at that innermost for
loop. 让我们来看看最里面
for
循环。 When you get the error, l
equals [2, 3]
. 当你得到错误时,
l
等于[2, 3]
。
The interpreter is telling you that 口译员告诉你
for a, b in [2, 3]:
print a
isn't going to work. 不会起作用。 Why doesn't
a
equal 2 and b
equal 3? 为什么没有
a
等于2和b
等于3? Well, what if you do this: 那么,如果你这样做怎么办:
for x in [2, 3]:
print x
What happens? 怎么了? First time through,
x
equals 2. Second time through, x
equals 3. Notice that in each iteration of the loop, you're just getting out one integer. 第一次通过,
x
等于2.第二次通过, x
等于3.注意,在循环的每次迭代中,你只是得到一个整数。 So if you replace x
with a, b
in the code, then the first time through the loop, the interpreter tries to do the following assignment: 因此,如果在代码中用
a, b
替换x
,那么第一次循环时,解释器会尝试执行以下赋值:
a, b = 2
and that fails because 2 isn't iterable. 并且失败因为2不可迭代。
The l
in that list comprehension is a single list, so iterating over it yields one and one value. 列表理解中的
l
是单个列表,因此迭代它会产生一个和一个值。 And you can't unpack that into two values. 而你无法将其解压缩为两个值。
print [a for i, l in amazing_list for a,b in [l]]
If you put it into something that can be unpacked, it will work. 如果你将它放入可以解压缩的东西中,它就会起作用。
(l,)
would also work. (l,)
也可以。
[a for a,b in [1, 2]]
this is basically the same thing. [a for a,b in [1, 2]]
这基本上是一回事。 And here it should be easy to see why it can't be done like that. 在这里应该很容易理解为什么不能这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.