串联Python列表时出现问题
问题描述:
我试图通过执行以下操作来连接两个列表,一个仅包含一个元素:
I am trying to concatenate two lists, one with just one element, by doing this:
print([6].append([1,1,0,0,0]))
但是,Python返回None
.我在做什么错了?
However, Python returns None
. What am I doing wrong?
答
使用+运算符
>>> [6] + [1,1,0,0,0]
[6, 1, 1, 0, 0, 0]
您尝试执行的操作是将一个列表追加到另一个列表中,这将导致
What you were attempting to do, is append a list onto another list, which would result in
>>> [6].append([1,1,0,0,0])
[6, [1,1,0,0,0]]
为什么看到None
返回,是因为.append
具有破坏性,修改了原始列表并返回了None
.它不会返回您要附加的列表.因此,您的列表 已被修改,但是您正在打印函数.append
的输出.
Why you are seeing None
returned, is because .append
is destructive, modifying the original list, and returning None
. It does not return the list that you're appending to. So your list is being modified, but you're printing the output of the function .append
.