I see you've already imported numpy because you're using np.linspace in the code. You are probably confusing numpy's abs, which will happily work on lists and arrays, with __builtin__.abs, which only works for scalars.
Change this:
abs(wf[0:100])
To this:
np.abs(wf[0:100])
Answer from wim on Stack OverflowI see you've already imported numpy because you're using np.linspace in the code. You are probably confusing numpy's abs, which will happily work on lists and arrays, with __builtin__.abs, which only works for scalars.
Change this:
abs(wf[0:100])
To this:
np.abs(wf[0:100])
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]]);
'Bad operand type for abs', how do i solve this
cryptography - How to correctly apply Python absolute value to a list to avoid an TypeError: bad operand type for abs(): 'list' - Stack Overflow
python - Why built-in functions like abs works on numpy array? - Stack Overflow
TypeError: bad operand type for abs(): 'NoneType' python error - Stack Overflow
That's because numpy.ndarray implements the __abs__(self) method. Just provide it for your own class, and abs() will magically work. For non-builtin types you can also provide this facility after-the-fact. E.g.
class A:
"A class without __abs__ defined"
def __init__(self, v):
self.v = v
def A_abs(a):
"An 'extension' method that will be added to `A`"
return abs(a.v)
# Make abs() work with an instance of A
A.__abs__ = A_abs
However, this will not work for built-in types, such as list or dict.
abs function looks for __abs__ method.
you can also, like numpy, implement the __abs__ method in your classes so abs would works with them.
i.e.
class A(object):
def __abs__(self):
return 8
>>> a= A()
>>> abs(a)
8
>>>
print("Enter a integer:") var1=input() print(abs(var1))
this is the error I get when I run it
TypeError: bad operand type for abs(): 'str'