Abs()的操作数类型错误:“列表" [英] bad operand type for abs(): 'list'

查看:105
本文介绍了Abs()的操作数类型错误:“列表"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在计算数组的每个值中的绝对值时,我遇到一个与abs()的错误操作数类型有关的错误:'list'.源代码中失败的部分是下一个:

When calculating the absolute value in each value of an array, I am getting an error related to bad operand type for abs(): 'list'. The part of source code which is failing is the next:

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函数中的此错误?我对此错误感到困惑:(

How can I fix this error in abs function? I got confused with this error :(

谢谢

推荐答案

我收集到您希望abs连同其他一些计算一起应用于列表切片的每个成员,因为您使用切片表示法.使用列表理解很容易.

I gather that you want abs applied to each member of the list slice along with some other computation, since you use slice notation. That's easy with a list comprehension.

plot(sf, [2.0/numSamples * abs(element) for element in wf[0:100]]);

这篇关于Abs()的操作数类型错误:“列表"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆