简体   繁体   English

为什么对于(看似)相同的代码我得到不同的输出?

[英]Why am I getting different outputs for the (seemingly) same code?

I'm trying to understand why these two examples are giving me different outputs. 我试图理解为什么这两个示例给我不同的输出。

Example1: 范例1:

list1 = [1,2,3,4,5]
list2 = []
for l in list1:
    list2.append(l)
print list2

#[1, 2, 3, 4, 5]

Example2: 范例2:

list1 = [1,2,3,4,5]
list2 = []
list2.append(l for l in list1)
print list2

#[<generator object <genexpr> at 0x10379ecd0>]

I've tried putting list() or tuple() after the append in the second example, but it's giving me one single element in the new list as opposed to 5 different ones. 在第二个示例中,我尝试将list()或tuple()放在附加位置之后,但是它在新列表中给了我一个元素,而不是5个不同的元素。

list2.append(tuple(l for l in list1))

#[(1, 2, 3, 4, 5)]

Would there be a way for me to get same same output from Example1 using just one line for the for loop? 是否有一种方法可以仅使用for循环的一行从Example1获得相同的输出?

I'd really appreciate any help! 我真的很感谢您的帮助!

You need extend instead of append for the second example 对于第二个示例,您需要extend而不是append

list1 = [1,2,3,4,5]
list2 = []
list2.extend(l for l in list1)  # or just list2.extend(list1)
print list2

If all you want to do is copy list1 then 如果您只想复制list1那么

list2 = list1[:]

or 要么

list2 = list(list1)

will do it 会做的

(l for l in list1) is a generator expression . (l for l in list1)是一个生成器表达式 It explicitly returns a generator, which is essentially a memory-saving iterator (think a list, but with each element returned on the fly, rather than stored all in memory). 它显式地返回一个生成器,该生成器本质上是一个节省内存的迭代器(考虑一个列表,但是每个元素都是动态返回的,而不是全部存储在内存中)。 The key to realising it is a generator expression is in noting that the expression is surrrounded by ( and ) . 实现它的关键是生成器表达式,要注意该表达式被()包围。

In list2.append(l for l in list1) , you have appended a single generator to the empty list. list2.append(l for l in list1)你已经附加了单台发电机的空单。 You can still get all the elements you want by doing for element in list2[0] , but it is not the same thing. 您仍然可以通过for element in list2[0]进行操作for element in list2[0]获取所需的所有元素,但这不是一回事。

However, in doing 但是,在做

for l in list1:
   list2.append(l)

no such generator expressions are encountered. 没有遇到这样的生成器表达式。 At every iteration - every run of the loop - l , an element, is appended to the end. 在每次迭代-循环的每次运行-元素l都附加到末尾。 Hence why this works the way you think. 因此,为什么这会按照您的想法起作用。


If you want to get your list2 using something similar to the generator expression, you want to use a list comprehension instead: 如果要使用类似于生成器表达式的方式获取list2 ,则想使用列表推导:

list2 = [l for l in list1]

Note that the difference between the structure of a list comprehension and a generator expression is simply the presence of square brackets [ , ] rather than parentheses. 请注意,列表理解和生成器表达式的结构之间的区别只是方括号[]而不是括号的存在。

You don't want to use append in the second example. 您不想在第二个示例中使用append。 Instead you want 相反,你想要

list2 = [l for l in list1]

There are easier ways to copy a list, but this is the one that most closely matches what you're going for in Example 2. 复制列表有更简单的方法,但这是与示例2中最匹配的方法。

If you don't like the nested list then you may simply use : 如果您不喜欢嵌套列表,则可以简单地使用:

list1 = [1,2,3,4,5]
list2 = []
[list2.append(l) for l in list1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM