为什么 append 方法在我的列表中返回 None ? [英] Why does the append method return None with my list?
本文介绍了为什么 append 方法在我的列表中返回 None ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道我可以使用append()"将元素添加到列表中,但为什么赋值返回 None?
<预><代码>>>>a=[1,2,3]>>>a.append(4)>>>打印一个[1, 2, 3, 4]>>>a=a.append(5)>>>打印一个没有任何>>> 解决方案
因为您将输出分配给没有输出的附加操作,所以代替:
a=a.append(5)
你只是想要
a.append(5)
I know I can use "append()" to add an element to a list, but why does the assignment return None?
>>> a=[1,2,3]
>>> a.append(4)
>>> print a
[1, 2, 3, 4]
>>> a=a.append(5)
>>> print a
None
>>>
解决方案
Because you're assigning an output to the act of appending which has no output, so instead of:
a=a.append(5)
you just want
a.append(5)
这篇关于为什么 append 方法在我的列表中返回 None ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文