Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/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 如何将数字添加到列表列表中?_Python_List_Sum_Element_Add - Fatal编程技术网

Python 如何将数字添加到列表列表中?

Python 如何将数字添加到列表列表中?,python,list,sum,element,add,Python,List,Sum,Element,Add,假设我有清单 [[1,2,3], [4,5,6]] 我想给每个元素添加5个 那么新的列表将是[[6,7,8],[9,10,11]]作为答案 def add(num, list): 只需执行嵌套列表理解(不要将list用作名称,它会覆盖内置项): 演示: 如果您愿意使用Numpy: import numpy as np np.array([[1,2,3], [4,5,6]]) + 5 将添加5元素返回: array([[ 6, 7, 8], [ 9, 10, 11]])

假设我有清单

[[1,2,3], [4,5,6]]
我想给每个元素添加5个

那么新的列表将是[[6,7,8],[9,10,11]]作为答案

def add(num, list):

只需执行嵌套列表理解(不要将
list
用作名称,它会覆盖内置项):

演示:


如果您愿意使用
Numpy

import numpy as np

np.array([[1,2,3], [4,5,6]]) + 5
将添加
5
元素返回:

array([[ 6,  7,  8],
       [ 9, 10, 11]])
map()

def add(num, lst):
    return [map(lambda x: x + num, l) for l in lst]
使用部分和运算符add进行小练习
操作员
模块中有
添加
操作员
functool
允许通过
partial
使用
map
在简单子列表上应用
add5
按要求使用嵌套列表 测量执行时间 使用已准备好的函数
add5

>>> %timeit [map(add5, sublst) for sublst in lst]
1000000 loops, best of 3: 1.23 µs per loop
使用建议的答案之一:

>>> [[item+5 for item in sub] for sub in lst]
[[6, 7, 8], [9, 10, 11]]

>>> %timeit [[item+5 for item in sub] for sub in lst]
1000000 loops, best of 3: 658 ns per loop
随着
add5
的创建,我们需要它:

>>> [map(partial(add, 5), sublst) for sublst in lst]
[[6, 7, 8], [9, 10, 11]]
>>> %timeit [map(partial(add, 5), sublst) for sublst in lst]
1000000 loops, best of 3: 1.5 µs per loop
将要添加的
num
设为变量:

>>> num = 5
>>> [map(partial(add, num), sublst) for sublst in lst]
[[6, 7, 8], [9, 10, 11]]
构建问题中所需的功能 在测试中,它是有效的

>>> add2lst(5, lst)
[[6, 7, 8], [9, 10, 11]]
结论-过于聪明可能会减慢速度 使用运算符和部分运算符的智能概念增加了执行的复杂性,这导致此解决方案的速度是简单解决方案的两倍多


所以:“保持简单”

您打算在某个时候编写一些代码,还是这样?哦,等等……Alxthon你的解决方案看起来很简单,当与我的“太聪明”的解决方案相比,使用部分,你的速度也快了两倍。@ USER 2581724,如果这解决了你的问题,请考虑接受这个答案,所以标准的方式说“谢谢”+ 1尼斯系列的实验,我同意结论。(除非对速度有迫切的需求,否则我会倾向于任何一种方式,“简单胜于复杂。”——Python的Zen)
>>> lst = [[1,2,3], [4,5,6]]
>>> [map(add5, sublst) for sublst in lst]
[[6, 7, 8], [9, 10, 11]]
>>> %timeit [map(add5, sublst) for sublst in lst]
1000000 loops, best of 3: 1.23 µs per loop
>>> [[item+5 for item in sub] for sub in lst]
[[6, 7, 8], [9, 10, 11]]

>>> %timeit [[item+5 for item in sub] for sub in lst]
1000000 loops, best of 3: 658 ns per loop
>>> [map(partial(add, 5), sublst) for sublst in lst]
[[6, 7, 8], [9, 10, 11]]
>>> %timeit [map(partial(add, 5), sublst) for sublst in lst]
1000000 loops, best of 3: 1.5 µs per loop
>>> num = 5
>>> [map(partial(add, num), sublst) for sublst in lst]
[[6, 7, 8], [9, 10, 11]]
>>> def add2lst(num, lst):
...     return [map(partial(add, num), sublst) for sublst in lst]
...
>>> add2lst(5, lst)
[[6, 7, 8], [9, 10, 11]]
>>> timeit.timeit("[[item+5 for item in sub] for sub in lst]", setup="from functools import partial;from operator import add;lst=[[1,2,3],[4,5,6]]", number=rep)
6.1195290088653564
>>> timeit.timeit("[map(partial(add, 5), sublst) for sublst in lst]", setup="from functools import partial;from operator import add;lst=[[1,2,3],[4,5,6]]", number=rep)
14.058987140655518