Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.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 有没有一种方法可以合并这样的列表?_Python_List - Fatal编程技术网

Python 有没有一种方法可以合并这样的列表?

Python 有没有一种方法可以合并这样的列表?,python,list,Python,List,假设a和b是列表 a=[[1],[2]] b=[[5,6,7],[3,4,5]] 我想得到一份清单 [[1,5,6,7], [2,3,4,5]] 有没有办法有效地做到这一点?列表或numpy数组都可以。zip是您的朋友: >>> a = [[1], [2]] >>> b = [[5, 6, 7], [3, 4, 5]] >>> [x+y for x, y in zip(a, b)] [[1, 5, 6, 7], [2, 3, 4, 5

假设a和b是列表

a=[[1],[2]]
b=[[5,6,7],[3,4,5]]
我想得到一份清单

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

有没有办法有效地做到这一点?列表或numpy数组都可以。

zip
是您的朋友:

>>> a = [[1], [2]]
>>> b = [[5, 6, 7], [3, 4, 5]]
>>> [x+y for x, y in zip(a, b)]
[[1, 5, 6, 7], [2, 3, 4, 5]]
您也可以使用
映射
operator
模块为此类用途提供了一个现成的
lambda x,y:x+y
定义

>>> import operator
>>> list(map(operator.add, a, b))