Python列表+列表与list.append()

时间:2022-02-23 00:36:00

Today I spent about 20 minutes trying to figure out why this worked as expected:

今天我花了大约20分钟试图弄清楚为什么它按预期工作:

users_stories_dict[a] = s + [b] 

but this would have a None value:

但是这将具有None值:

users_stories_dict[a] = s.append(b)

Anyone know why the append function does not return the new list? I'm looking for some sort of sensible reason this decision was made; it looks like a Python novice gotcha to me right now.

任何人都知道为什么append函数不返回新列表?我正在寻找做出这个决定的某种合理理由;它现在看起来像Python新手。

2 个解决方案

#1


10  

append works by actually modifying a list, and so all the magic is in side-effects. Accordingly, the result returned by append is None. In other words, what one wants is:

通过实际修改列表来追加工作,所以所有的魔法都是副作用。因此,append返回的结果是None。换句话说,人们想要的是:

s.append(b)

and then:

users_stories_dict[a] = s

users_stories_dict [a] = s

But, you've already figured that much out. As to why it was done this way, while I don't really know, my guess is that it might have something to do with a 0 (or false) exit value indicating that an operation proceeded normally, and by returning None for functions whose role is to modify their arguments in-place you report that the modification succeeded.

但是,你已经想出了那么多。至于为什么这样做,虽然我真的不知道,但我的猜测是它可能与0(或错误)退出值有关,表明操作正常进行,并且对于其函数返回None角色是在适当的位置修改其参数,并报告修改成功。

But I agree that it would be nice if it returned the modified list back. At least, Python's behavior is consistent across all such functions.

但我同意如果它返回修改后的列表会很好。至少,Python的行为在所有这些功能中都是一致的。

#2


8  

The append() method returns a None, because it modifies the list it self by adding the object appended as an element, while the + operator concatenates the two lists and return the resulting list

append()方法返回None,因为它通过添加作为元素追加的对象来修改它自己的列表,而+运算符连接两个列表并返回结果列表

eg:

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

print a+b         # returns a list made by concatenating the lists a and b
>>> [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

print a.append(b) # Adds the list b as element at the end of the list a and returns None
>>> None

print a           # the list a was modified during the last append call and has the list b as last element
>>> [1, 2, 3, 4, 5, [6, 7, 8, 9, 0]]

So as you can see the easiest way is just to add the two lists together as even if you append the list b to a using append() you will not get the result you want without additional work

因此,您可以看到最简单的方法就是将两个列表一起添加,因为即使您将列表b附加到使用append(),您也无法获得所需的结果而无需额外的工作

#1


10  

append works by actually modifying a list, and so all the magic is in side-effects. Accordingly, the result returned by append is None. In other words, what one wants is:

通过实际修改列表来追加工作,所以所有的魔法都是副作用。因此,append返回的结果是None。换句话说,人们想要的是:

s.append(b)

and then:

users_stories_dict[a] = s

users_stories_dict [a] = s

But, you've already figured that much out. As to why it was done this way, while I don't really know, my guess is that it might have something to do with a 0 (or false) exit value indicating that an operation proceeded normally, and by returning None for functions whose role is to modify their arguments in-place you report that the modification succeeded.

但是,你已经想出了那么多。至于为什么这样做,虽然我真的不知道,但我的猜测是它可能与0(或错误)退出值有关,表明操作正常进行,并且对于其函数返回None角色是在适当的位置修改其参数,并报告修改成功。

But I agree that it would be nice if it returned the modified list back. At least, Python's behavior is consistent across all such functions.

但我同意如果它返回修改后的列表会很好。至少,Python的行为在所有这些功能中都是一致的。

#2


8  

The append() method returns a None, because it modifies the list it self by adding the object appended as an element, while the + operator concatenates the two lists and return the resulting list

append()方法返回None,因为它通过添加作为元素追加的对象来修改它自己的列表,而+运算符连接两个列表并返回结果列表

eg:

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

print a+b         # returns a list made by concatenating the lists a and b
>>> [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

print a.append(b) # Adds the list b as element at the end of the list a and returns None
>>> None

print a           # the list a was modified during the last append call and has the list b as last element
>>> [1, 2, 3, 4, 5, [6, 7, 8, 9, 0]]

So as you can see the easiest way is just to add the two lists together as even if you append the list b to a using append() you will not get the result you want without additional work

因此,您可以看到最简单的方法就是将两个列表一起添加,因为即使您将列表b附加到使用append(),您也无法获得所需的结果而无需额外的工作