在 Python 中将列表附加到自身
问题描述:
我想给自己附加一个列表,我认为这会奏效:
I want to attach a list to itself and I thought this would work:
x = [1,2]
y = x.extend(x)
print y
我想找回 [1,2,1,2]
但我得到的只是内置的 None
.我究竟做错了什么?我使用的是 Python v2.6
I wanted to get back [1,2,1,2]
but all I get back is the builtin None
. What am I doing wrong? I'm using Python v2.6
答
x.extend(x)
不返回新副本,而是修改列表本身.
x.extend(x)
does not return a new copy, it modifies the list itself.
只需打印 x
.
你也可以使用 x + x