Answers:
Python 3:使用functools.reduce:
>>> from functools import reduce
>>> reduce(lambda x, y: x*y, [1,2,3,4,5,6])
720Python 2:使用reduce:
>>> reduce(lambda x, y: x*y, [1,2,3,4,5,6])
720为了与2和3兼容,请使用pip install six:
>>> from six.moves import reduce
>>> reduce(lambda x, y: x*y, [1,2,3,4,5,6])
720lambda平均重复数为.02s / 1000,而operator.mul平均重复数为.009s / 1000,operator.mul加快了一个数量级。
                    operator.mul直接转到C.
                    math.prod([1,2,3,4,5,6])。(需要导入课程)
                    您可以使用:
import operator
import functools
functools.reduce(operator.mul, [1,2,3,4,5,6], 1)有关说明,请参见reduce和operator.mul文档。
您需要import functoolsPython 3+中的代码行。
reduce()函数已从全局名称空间中删除,并放置在functools模块中。因此,在python3中,您需要说from functools import reduce。
                    我会使用numpy.prod来执行任务。见下文。
import numpy as np
mylist = [1, 2, 3, 4, 5, 6] 
result = np.prod(np.array(mylist))  result = np.prod(mylist)
                    numpy.int32上面   的默认值时2)对于小列表,这会明显变慢,因为NumPy需要分配一个数组(如果经常重复,则是相关的)
                    np.prod(np.array(range(1,21)))
                    reduce。
                    如果要避免导入任何内容并避免使用更复杂的Python区域,则可以使用简单的for循环
product = 1  # Don't use 0 here, otherwise, you'll get zero 
             # because anything times zero will be zero.
list = [1, 2, 3]
for x in list:
    product *= x从开始Python 3.8,.prod函数已经包含math在标准库的模块中:
math.prod(iterable, *, start=1)
该方法返回一个start值(默认值:1)乘以数字可迭代的乘积:
import math
math.prod([1, 2, 3, 4, 5, 6])
>>> 720如果iterable为空,则将产生1(或start提供值,如果提供)。
这是我机器上的一些性能指标。与在长时间运行的循环中对较小的输入执行此操作有关:
import functools, operator, timeit
import numpy as np
def multiply_numpy(iterable):
    return np.prod(np.array(iterable))
def multiply_functools(iterable):
    return functools.reduce(operator.mul, iterable)
def multiply_manual(iterable):
    prod = 1
    for x in iterable:
        prod *= x
    return prod
sizesToTest = [5, 10, 100, 1000, 10000, 100000]
for size in sizesToTest:
    data = [1] * size
    timerNumpy = timeit.Timer(lambda: multiply_numpy(data))
    timerFunctools = timeit.Timer(lambda: multiply_functools(data))
    timerManual = timeit.Timer(lambda: multiply_manual(data))
    repeats = int(5e6 / size)
    resultNumpy = timerNumpy.timeit(repeats)
    resultFunctools = timerFunctools.timeit(repeats)
    resultManual = timerManual.timeit(repeats)
    print(f'Input size: {size:>7d} Repeats: {repeats:>8d}    Numpy: {resultNumpy:.3f}, Functools: {resultFunctools:.3f}, Manual: {resultManual:.3f}')结果:
Input size:       5 Repeats:  1000000    Numpy: 4.670, Functools: 0.586, Manual: 0.459
Input size:      10 Repeats:   500000    Numpy: 2.443, Functools: 0.401, Manual: 0.321
Input size:     100 Repeats:    50000    Numpy: 0.505, Functools: 0.220, Manual: 0.197
Input size:    1000 Repeats:     5000    Numpy: 0.303, Functools: 0.207, Manual: 0.185
Input size:   10000 Repeats:      500    Numpy: 0.265, Functools: 0.194, Manual: 0.187
Input size:  100000 Repeats:       50    Numpy: 0.266, Functools: 0.198, Manual: 0.185您会看到Numpy在较小的输入上要慢得多,因为它在执行乘法之前会分配一个数组。另外,请注意Numpy中的溢出。
multiply_functools,并multiply_numpy  具有以查找被拖累np,functools以及operator全局,其次属性查找。您介意切换到当地人吗?_reduce=functools.reduce, _mul = operator.mul`在函数签名然后return _reduce(_mul, iterable)在体内,等等
                    np.prod()选项启动速度最快达到100个元素或更多。
                    我个人对将通用列表的所有元素相乘的函数喜欢这样:
def multiply(n):
    total = 1
    for i in range(0, len(n)):
        total *= n[i]
    print total它紧凑,使用简单的东西(变量和for循环),并且对我来说很直观(看起来就像我对问题的看法,只用一个,乘以它,然后乘以下一个,依此类推! )
for i in n:,那么total *= i?会不会简单得多?
                    我想通过以下方式:
    def product_list(p):
          total =1 #critical step works for all list
          for i in p:
             total=total*i # this will ensure that each elements are multiplied by itself
          return total
   print product_list([2,3,4,2]) #should print 48我的解决方案:
def multiply(numbers):
    a = 1
    for num in numbers:
        a *= num
        return a
  pass'''了解循环逻辑使用的唯一简单方法'''
对膝中的i而言,lap = [2,5,7,7,9] x = 1:x = i * x print(x)
很简单,不导入任何内容。这是我的代码。这将定义一个将列表中所有项目相乘并返回其乘积的函数。
def myfunc(lst):
    multi=1
    for product in lst:
        multi*=product
    return product