阅读量:117
有以下替代方案:
- 使用循环实现,逐个相乘:
def custom_prod(arr):
result = 1
for num in arr:
result *= num
return result
- 使用reduce函数实现:
from functools import reduce
def custom_prod(arr):
return reduce(lambda x, y: x * y, arr)
- 使用numpy库中的prod函数:
import numpy as np
def custom_prod(arr):
return np.prod(arr)
- 使用math库中的prod函数:
import math
def custom_prod(arr):
return math.prod(arr)