在Python中为其自身添加一个列表 [英] Appending a list to itself in Python
本文介绍了在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
这篇关于在Python中为其自身添加一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文