在python中将一个列表插入另一个列表的语法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3748063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:31:48  来源:igfitidea点击:

What is the syntax to insert one list into another list in python?

pythonlistappendextend

提问by Compuser7

Given two lists:

给出两个列表:

x = [1,2,3]
y = [4,5,6]

What is the syntax to:

什么是语法:

  1. Insert xinto ysuch that ynow looks like [1, 2, 3, [4, 5, 6]]?
  2. Insert all the items of xinto ysuch that ynow looks like [1, 2, 3, 4, 5, 6]?
  1. 插入xy这样y现在看起来像[1, 2, 3, [4, 5, 6]]
  2. 插入的所有项目x进入y,使得y现在的样子[1, 2, 3, 4, 5, 6]

采纳答案by Paolo Bergantino

Do you mean append?

你的意思是append

>>> x = [1,2,3]
>>> y = [4,5,6]
>>> x.append(y)
>>> x
[1, 2, 3, [4, 5, 6]]

Or merge?

还是合并?

>>> x = [1,2,3]
>>> y = [4,5,6]
>>> x + y
[1, 2, 3, 4, 5, 6]
>>> x.extend(y)
>>> x
[1, 2, 3, 4, 5, 6] 

回答by Coding District

foo = [1, 2, 3]
bar = [4, 5, 6]

foo.append(bar) --> [1, 2, 3, [4, 5, 6]]
foo.extend(bar) --> [1, 2, 3, 4, 5, 6]

http://docs.python.org/tutorial/datastructures.html

http://docs.python.org/tutorial/datastructures.html

回答by Sergey

The question does not make clear what exactly you want to achieve.

这个问题并没有说明你到底想要达到什么目的。

List has the appendmethod, which appends its argument to the list:

List 有一个append方法,它将它的参数附加到列表中:

>>> list_one = [1,2,3]
>>> list_two = [4,5,6]
>>> list_one.append(list_two)
>>> list_one
[1, 2, 3, [4, 5, 6]]

There's also the extendmethod, which appends itemsfrom the list you pass as an argument:

还有一个extend方法,它从您作为参数传递的列表中附加项目

>>> list_one = [1,2,3]
>>> list_two = [4,5,6]
>>> list_one.extend(list_two)
>>> list_one
[1, 2, 3, 4, 5, 6]

And of course, there's the insertmethod which acts similarly to appendbut allows you to specify the insertion point:

当然,还有一个insert方法类似于append但允许您指定插入点:

>>> list_one.insert(2, list_two)
>>> list_one
[1, 2, [4, 5, 6], 3, 4, 5, 6]

To extend a list at a specific insertion point you can use list slicing (thanks, @florisla):

要在特定插入点扩展列表,您可以使用列表切片(感谢,@florisla):

>>> l = [1, 2, 3, 4, 5]
>>> l[2:2] = ['a', 'b', 'c']
>>> l
[1, 2, 'a', 'b', 'c', 3, 4, 5]

List slicing is quite flexible as it allows to replace a range of entries in a list with a range of entries from another list:

列表切片非常灵活,因为它允许用另一个列表中的一系列条目替换列表中的一系列条目:

>>> l = [1, 2, 3, 4, 5]
>>> l[2:4] = ['a', 'b', 'c'][1:3]
>>> l
[1, 2, 'b', 'c', 5]

回答by squal

If you want to add the elements in a list (list2) to the end of other list (list), then you can use the list extend method

如果你想把一个列表(list2)中的元素添加到另一个列表(list)的末尾,那么你可以使用列表扩展方法

list = [1, 2, 3]
list2 = [4, 5, 6]
list.extend(list2)
print list
[1, 2, 3, 4, 5, 6]

Or if you want to concatenate two list then you can use + sign

或者,如果您想连接两个列表,则可以使用 + 号

list3 = list + list2
print list3
[1, 2, 3, 4, 5, 6]

回答by user3707850

You can also just do...

你也可以只做...

x += y

回答by Raj Stha

If we just do x.append(y), y gets referenced into x such that any changes made to y will affect appended x as well. So if we need to insert only elements, we should do following:

如果我们只是这样做x.append(y), y 就会被引用到 x 中,这样对 y 所做的任何更改也会影响附加的 x。所以如果我们只需要插入元素,我们应该这样做:

x = [1,2,3] y = [4,5,6] x.append(y[:])

x = [1,2,3] y = [4,5,6] x.append(y[:])