将列表元素插入列表列表的Pythonic方法?

马塞洛·扎拉特(Marcelo Zarate)

例如,让我们来:

nodes = [[1, 2],[3, 4]] 
thelist = [[5, 6], [7, 8]]

我该如何编码,因此清单将是:

[[1, 2],[3, 4],[5, 6],[7, 8]]

我知道该怎么做,但是我想要一个优雅的python方法。

我的尝试:

for node in nodes:
    thelist.insert(0, node)

我猜应该有一种更Python化的方式来做到这一点。

编辑:顺序在某种程度上很重要(这就是为什么我尝试在索引0处插入)。

安迪·海登(Andy Hayden)

只需将它们添加在一起:

In [11]: nodes + thelist
Out[11]: [[1, 2], [3, 4], [5, 6], [7, 8]]

您还可以使用扩展(修改节点):

In [12]: nodes.extend(thelist)

In [13]: nodes
Out[13]: [[1, 2], [3, 4], [5, 6], [7, 8]]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章