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

214544 观看

6回复

1914 作者的声誉

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

1. 插入`x``y`这样`y`现在看起来像`[1, 2, 3, [4, 5, 6]]`
2. 插入的所有项目`x`进入`y`，使得`y`现在的样子`[1, 2, 3, 4, 5, 6]`

### 回应 (6)

29

10036 作者的声誉

``````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

320

382740 作者的声誉

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

``````>>> 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]
``````

55

7463 作者的声誉

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]]
``````

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

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

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

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

2

105 作者的声誉

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

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

2

33 作者的声誉

``````x += y
``````

0

106 作者的声誉

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