在计算数组每个值的绝对值时,出现与 abs(): ‘list’ 的错误操作数类型相关的错误。失败的源代码部分是下一个:
x = amplitudex * sin((2 * pi * (frequency * 1) * t) + phase);
y = amplitudey * sin((2 * pi * (frequency * 2) * t) + phase);
z = amplitudez * sin((2 * pi * (frequency * 3) * t) + phase);
w= 0.55* (x + y + z);
....
n = len(w);
wf = [float(0)] * n;
for k in range(n): # For each output element
s = float(0);
for t in range(n): # For each input element
s += w[t] * cmath.exp(-2j * cmath.pi * t * k / n);
wf[k] = float(s);
sf = np.linspace(0.0, 1.0/(2.0*numCycles), numSamples/2);
#The calculation of absolute values causes error:
plot(sf, 2.0/numSamples * abs(wf[0:100]));
如何修复 abs 函数中的这个错误?我对这个错误感到困惑:(
谢谢
原文由 John Barton 发布,翻译遵循 CC BY-SA 4.0 许可协议
我了解到您想要
abs
应用于列表切片的每个成员以及一些其他计算,因为您使用切片表示法。使用 list comprehension 这很容易。