Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在python中,如何将两个嵌套列表附加到单个嵌套列表中_Python 3.x_List_Nested Lists - Fatal编程技术网

Python 3.x 在python中,如何将两个嵌套列表附加到单个嵌套列表中

Python 3.x 在python中,如何将两个嵌套列表附加到单个嵌套列表中,python-3.x,list,nested-lists,Python 3.x,List,Nested Lists,我有两个嵌套列表,如: a = [[2,3,4],[3,5,6]] b = [[4,5], [5,6,7,7,7]] 我需要将两个嵌套列表附加到单个嵌套列表中 预期产出: [[4, 5], [5, 6, 7, 7, 7], [2, 3, 4], [3, 5, 6]] 我试过这样做 a = [[2,3,4],[3,5,6]] b = [[4,5], [5,6,7,7,7]] b.append(a) print(b) 我得到的输出: [[4, 5], [5, 6, 7, 7, 7], [[2,

我有两个嵌套列表,如:

a = [[2,3,4],[3,5,6]]
b = [[4,5], [5,6,7,7,7]]
我需要将两个嵌套列表附加到单个嵌套列表中

预期产出:

[[4, 5], [5, 6, 7, 7, 7], [2, 3, 4], [3, 5, 6]]
我试过这样做

a = [[2,3,4],[3,5,6]]
b = [[4,5], [5,6,7,7,7]]
b.append(a)
print(b)
我得到的输出:

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

任何建议都会有帮助

只需创建一个新列表:

a=[[2,3,4],[3,5,6]]
b=[[4,5],[5,6,7,7,7]]
c=a+b
# [[2, 3, 4], [3, 5, 6], [4, 5], [5, 6, 7, 7, 7]]

使用
.extend
,给定

a = [[2,3,4],[3,5,6]]
b = [[4,5], [5,6,7,7,7]]
b.extend(a)
注意
.extend
方法扩展现有列表,所做的更改位于执行.extend的列表中,因此此处对b进行了更改

输出

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

拆包是一种方法:

c = [*a, *b]
# [[2, 3, 4], [3, 5, 6], [4, 5], [5, 6, 7, 7, 7]]

使用
list
对象的
extend
方法。