我一直在尝试在Python中乘以上一个和下一个数字,但是我总是遇到不同的错误。这就是我一直在做的:
u=[1, 41, 56, 80]
def Filter(vel):
global firstDerivative
firstDerivative = np.repeat(None, len(vel))
for index, obj in enumerate(vel):
if index !=0 & index !=len(vel-1):
firstDerivative = (vel[index-1]*vel[index+1])/2
Filter(u)
出现以下错误:
TypeError: unsupported operand type(s) for -: 'list' and 'int'
我实际上也尝试过地图,但id无效。
最佳答案
您需要从1迭代到-1,并在进行时填充firstDerivative
数组:
def Filter(vel):
global firstDerivative # you are probably better advised to return firstDerivative instead
firstDerivative = [0] * (len(vel) - 2)
for ndx in range(1, len(vel) - 1):
firstDerivative[ndx-1] = vel[ndx-1] * vel[ndx+1] / 2
firstDerivative = []
u = [1, 2, 3, 4, 5, 6]
Filter(u)
firstDerivative
输出:
对于一阶导数:
[1.5, 4.0, 7.5, 12.0]
关于python - 乘以Python中的上一个和下一个元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47505522/