返回列表的产品 [英] Returning the product of a list
本文介绍了返回列表的产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有更简洁、高效或简单的 Pythonic 方法来执行以下操作?
def product(lst):p = 1因为我在 lst:p *= i返回 p
我实际上发现这比使用 operator.mul 稍微快一点:
from 操作符 import mul# from functools import reduce # python3 兼容性def with_lambda(lst):减少(lambda x,y:x * y,lst)def without_lambda(lst):减少(多,lst)def forloop(lst):r = 1对于 lst 中的 x:r *= x返回导入时间a = 范围(50)b = range(1,50)#没有零t = timeit.Timer("with_lambda(a)", "from __main__ import with_lambda,a")打印(带有 lambda:",t.timeit())t = timeit.Timer("without_lambda(a)", "from __main__ import without_lambda,a")打印(没有lambda:",t.timeit())t = timeit.Timer("forloop(a)", "from __main__ import forloop,a")打印(for循环:",t.timeit())t = timeit.Timer("with_lambda(b)", "from __main__ import with_lambda,b")打印(带有 lambda(无 0):",t.timeit())t = timeit.Timer("without_lambda(b)", "from __main__ import without_lambda,b")打印(没有 lambda(没有 0):",t.timeit())t = timeit.Timer("forloop(b)", "from __main__ import forloop,b")打印(for循环(无0):",t.timeit())
给我
('with lambda:', 17.755449056625366)('没有 lambda:', 8.2084708213806152)('for 循环:', 7.4836349487304688)('带 lambda (无 0):', 22.570688009262085)('没有 lambda (no 0):', 12.472226858139038)('for 循环 (no 0):', 11.04065990447998)
解决方案
不使用 lambda:
from 操作符 import mul减少(多,列表,1)
它更好更快.使用 python 2.7.5
from 操作符 import mul将 numpy 导入为 np导入 numexpr 作为 ne# from functools import reduce # python3 兼容性a = 范围(1, 101)%timeit reduce(lambda x, y: x * y, a) # (1)%timeit reduce(mul, a) # (2)%timeit np.prod(a) # (3)%timeit ne.evaluate("prod(a)") # (4)
在以下配置中:
a = range(1, 101) # Aa = np.array(a) # Ba = np.arange(1, 1e4, dtype=int) #Ca = np.arange(1, 1e5, dtype=float) #D
python 2.7.5 的结果
<前>|1 |2 |3 |4 |-------+-----------+-----------+-----------+-----------+A 20.8 µs 13.3 µs 22.6 µs 39.6 µsB 106 微秒 95.3 微秒 5.92 微秒 26.1 微秒C 4.34 毫秒 3.51 毫秒 16.7 微秒 38.9 微秒D 46.6 毫秒 38.5 毫秒 180 微秒 216 微秒结果:np.prod
是最快的,如果你使用 np.array
作为数据结构(小数组 18x,大数组 250x)
使用 python 3.3.2:
<前>|1 |2 |3 |4 |-------+-----------+-----------+-----------+-----------+A 23.6 µs 12.3 µs 68.6 µs 84.9 µsB 133 微秒 107 微秒 7.42 微秒 27.5 微秒C 4.79 毫秒 3.74 毫秒 18.6 微秒 40.9 微秒D 48.4 毫秒 36.8 毫秒 187 微秒 214 微秒python 3 慢吗?
Is there a more concise, efficient or simply pythonic way to do the following?
def product(lst):
p = 1
for i in lst:
p *= i
return p
EDIT:
I actually find that this is marginally faster than using operator.mul:
from operator import mul
# from functools import reduce # python3 compatibility
def with_lambda(lst):
reduce(lambda x, y: x * y, lst)
def without_lambda(lst):
reduce(mul, lst)
def forloop(lst):
r = 1
for x in lst:
r *= x
return r
import timeit
a = range(50)
b = range(1,50)#no zero
t = timeit.Timer("with_lambda(a)", "from __main__ import with_lambda,a")
print("with lambda:", t.timeit())
t = timeit.Timer("without_lambda(a)", "from __main__ import without_lambda,a")
print("without lambda:", t.timeit())
t = timeit.Timer("forloop(a)", "from __main__ import forloop,a")
print("for loop:", t.timeit())
t = timeit.Timer("with_lambda(b)", "from __main__ import with_lambda,b")
print("with lambda (no 0):", t.timeit())
t = timeit.Timer("without_lambda(b)", "from __main__ import without_lambda,b")
print("without lambda (no 0):", t.timeit())
t = timeit.Timer("forloop(b)", "from __main__ import forloop,b")
print("for loop (no 0):", t.timeit())
gives me
('with lambda:', 17.755449056625366)
('without lambda:', 8.2084708213806152)
('for loop:', 7.4836349487304688)
('with lambda (no 0):', 22.570688009262085)
('without lambda (no 0):', 12.472226858139038)
('for loop (no 0):', 11.04065990447998)
解决方案
Without using lambda:
from operator import mul
reduce(mul, list, 1)
it is better and faster. With python 2.7.5
from operator import mul
import numpy as np
import numexpr as ne
# from functools import reduce # python3 compatibility
a = range(1, 101)
%timeit reduce(lambda x, y: x * y, a) # (1)
%timeit reduce(mul, a) # (2)
%timeit np.prod(a) # (3)
%timeit ne.evaluate("prod(a)") # (4)
In the following configuration:
a = range(1, 101) # A
a = np.array(a) # B
a = np.arange(1, 1e4, dtype=int) #C
a = np.arange(1, 1e5, dtype=float) #D
Results with python 2.7.5
| 1 | 2 | 3 | 4 | -------+-----------+-----------+-----------+-----------+ A 20.8 µs 13.3 µs 22.6 µs 39.6 µs B 106 µs 95.3 µs 5.92 µs 26.1 µs C 4.34 ms 3.51 ms 16.7 µs 38.9 µs D 46.6 ms 38.5 ms 180 µs 216 µs
Result: np.prod
is the fastest one, if you use np.array
as data structure (18x for small array, 250x for large array)
with python 3.3.2:
| 1 | 2 | 3 | 4 | -------+-----------+-----------+-----------+-----------+ A 23.6 µs 12.3 µs 68.6 µs 84.9 µs B 133 µs 107 µs 7.42 µs 27.5 µs C 4.79 ms 3.74 ms 18.6 µs 40.9 µs D 48.4 ms 36.8 ms 187 µs 214 µs
Is python 3 slower?
这篇关于返回列表的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文