【发布时间】:2018-12-05 21:45:21
【问题描述】:
对此感到抱歉。我是 Python 新手,正在做一个 leetcode 问题,我目前正在尝试将列表中的所有数字相乘以获得最终结果。这是我的代码:
import numpy
class Solution:
def productExceptSelf(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
arr = []
for i in range(len(nums)):
temp = nums[:i] + nums[i + 1 : len(nums)]
result = numpy.prod(temp)
arr.append(result)
return arr
但是我得到这个错误:
Line 56: Exception: Type <class 'numpy.int64'>: Not implemented
有没有其他方法可以将列表中的所有元素相乘并存储在一个值中。
【问题讨论】:
-
第 56 行是哪一行?
-
让我们看看我是否可以通过智能手机做到这一点:
import operator; from functools import reduce; multiply_all = lambda series: reduce(series, operator.__mul__) -
@MateenUlhaq 不太确定,因为这是在 leet 代码中
-
能否请您发布完整的代码?
-
我猜你不能在 leetcode 中使用
numpy。
标签: python python-3.x list numpy