sum()的功能是什么,但要乘法呢?产品()?

问题:sum()的功能是什么,但要乘法呢?产品()?

Python的sum()函数返回一个可迭代的数字之和。

sum([3,4,5]) == 3 + 4 + 5 == 12

我正在寻找返回产品的函数。

somelib.somefunc([3,4,5]) == 3 * 4 * 5 == 60

我很确定存在这样的功能,但是找不到。

Python’s sum() function returns the sum of numbers in an iterable.

sum([3,4,5]) == 3 + 4 + 5 == 12

I’m looking for the function that returns the product instead.

somelib.somefunc([3,4,5]) == 3 * 4 * 5 == 60

I’m pretty sure such a function exists, but I can’t find it.


回答 0

更新:

在Python 3.8中,prod函数已添加到math模块。请参阅:math.prod()

较早的信息:Python 3.7及更低版本

您要查找的函数称为prod()product(),但Python没有该函数。因此,您需要编写自己的代码(很简单)。

在prod()上的发音

是的,这是对的。Guido 拒绝了内置prod()函数的想法,因为他认为很少需要它。

用reduce()替代

正如您建议的那样,使用reduce()operator.mul()制作自己的东西并不难:

from functools import reduce  # Required in Python 3
def prod(iterable):
    return reduce(operator.mul, iterable, 1)

>>> prod(range(1, 5))
24

请注意,在Python 3中,reduce()函数已移至functools模块

具体情况:阶乘

附带说明一下,prod()的主要动机用例是计算阶乘。我们已经在math模块中对此提供了支持:

>>> import math

>>> math.factorial(10)
3628800

对数的替代

如果您的数据由浮点数组成,则可以使用带有指数和对数的sum()来计算乘积:

>>> from math import log, exp

>>> data = [1.2, 1.5, 2.5, 0.9, 14.2, 3.8]
>>> exp(sum(map(log, data)))
218.53799999999993

>>> 1.2 * 1.5 * 2.5 * 0.9 * 14.2 * 3.8
218.53799999999998

注意,使用log()要求所有输入均为正。

Update:

In Python 3.8, the prod function was added to the math module. See: math.prod().

Older info: Python 3.7 and prior

The function you’re looking for would be called prod() or product() but Python doesn’t have that function. So, you need to write your own (which is easy).

Pronouncement on prod()

Yes, that’s right. Guido rejected the idea for a built-in prod() function because he thought it was rarely needed.

Alternative with reduce()

As you suggested, it is not hard to make your own using reduce() and operator.mul():

from functools import reduce  # Required in Python 3
import operator
def prod(iterable):
    return reduce(operator.mul, iterable, 1)

>>> prod(range(1, 5))
24

Note, in Python 3, the reduce() function was moved to the functools module.

Specific case: Factorials

As a side note, the primary motivating use case for prod() is to compute factorials. We already have support for that in the math module:

>>> import math

>>> math.factorial(10)
3628800

Alternative with logarithms

If your data consists of floats, you can compute a product using sum() with exponents and logarithms:

>>> from math import log, exp

>>> data = [1.2, 1.5, 2.5, 0.9, 14.2, 3.8]
>>> exp(sum(map(log, data)))
218.53799999999993

>>> 1.2 * 1.5 * 2.5 * 0.9 * 14.2 * 3.8
218.53799999999998

Note, the use of log() requires that all the inputs are positive.


回答 1

实际上,Guido否决了这个想法:http : //bugs.python.org/issue1093

但是,正如该期杂志所述,您可以轻松制作一个:

from functools import reduce # Valid in Python 2.6+, required in Python 3
import operator

reduce(operator.mul, (3, 4, 5), 1)

Actually, Guido vetoed the idea: http://bugs.python.org/issue1093

But, as noted in that issue, you can make one pretty easily:

from functools import reduce # Valid in Python 2.6+, required in Python 3
import operator

reduce(operator.mul, (3, 4, 5), 1)

回答 2

没有一个内置的,但其实很简单推出自己的,这表现在这里

import operator
def prod(factors):
    return reduce(operator.mul, factors, 1)

查看此问题的答案:

哪个Python模块适合列表中的数据操作?

There isn’t one built in, but it’s simple to roll your own, as demonstrated here:

import operator
def prod(factors):
    return reduce(operator.mul, factors, 1)

See answers to this question:

Which Python module is suitable for data manipulation in a list?


回答 3

有一个prod()在numpy的,做你问什么。

There’s a prod() in numpy that does what you’re asking for.


回答 4

Numeric.product 

( 要么

reduce(lambda x,y:x*y,[3,4,5])

Numeric.product 

( or

reduce(lambda x,y:x*y,[3,4,5])

)


回答 5

用这个

def prod(iterable):
    p = 1
    for n in iterable:
        p *= n
    return p

由于没有内置prod功能。

Use this

def prod(iterable):
    p = 1
    for n in iterable:
        p *= n
    return p

Since there’s no built-in prod function.


回答 6

我更喜欢使用functools.reduce()和上面的答案ab使用numpy.prod()答案,但这是使用itertools.accumulate()的另一种解决方案:

import itertools
import operator
prod = list(itertools.accumulate((3, 4, 5), operator.mul))[-1]

I prefer the answers a and b above using functools.reduce() and the answer using numpy.prod(), but here is yet another solution using itertools.accumulate():

import itertools
import operator
prod = list(itertools.accumulate((3, 4, 5), operator.mul))[-1]

回答 7

也许不是“内置”,但我认为它是内置的。无论如何,请使用numpy

import numpy 
prod_sum = numpy.prod(some_list)

Perhaps not a “builtin”, but I consider it builtin. anyways just use numpy

import numpy 
prod_sum = numpy.prod(some_list)