追加新项目列表的列表中 [英] Append a new item to a list within a list
本文介绍了追加新项目列表的列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图另一个列表中的一个新的float元素添加到列表,例如:
I'm trying to append a new float element to a list within another list, for example:
list = [[]]*2
list[1].append(2.5)
和我得到以下几点:
print list
[[2.5], [2.5]]
当我想获得:
[[], [2.5]]
我怎样才能做到这一点?
How can I do this?
先谢谢了。
推荐答案
LST = [[]为_中的xrange(2)]
(或只是 [] []]
)。不要用乘法可变对象 - 你得到同样的一条X次,不是X不同的人
lst = [[] for _ in xrange(2)]
(or just [[], []]
). Don't use multiplication with mutable objects — you get the same one X times, not X different ones.
这篇关于追加新项目列表的列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文