PYTHON - Typeerror: 'numpy.float64' Object is not iterable

Asked

Viewed 770 times

0

Hello, I’m getting this error return while trying to run the following loop:

for p in p:
    for q in p:
        if p != q:
            arrFreq.append(p - q)

OBS: "p" is an array that contains frequencies of an audio. The final idea of this for is to store the difference between frequencies. The "if" is not to pass the same value in the array. For example, you have to calculate the difference of the p[0], p[1]... Can’t calculate p[0] with p[0].

  • I don’t know what you wanted with this code - but it’s very wrong. if the variables were right in "for", you’d have one arrFreq with the square of the number of frequencies of your original sample, with the differences between all of them, as in a tabuada - which obviously would not serve for anything.

  • If you have two Numpy arrays of the same size, to subtract one from the other and have a new array, just use "-" - nor need any: diferencas = p -q

2 answers

1

You are overwriting your variable p (the initial array) when calling each element inside it as p also.

That way, on the line for q in p:, p is no longer your initial array, but one of its elements. As the array elements are numbers (of the type numpy.float64), and numbers are not eternal, you see the mistake.

Simply choose another name for the variable in your loop to fix this, for example:

for elemento_01 in p:
    for elemento_02 in p:
        if elemento_01 != elemento_02:
            arrFreq.append(elemento_01 - elemento_02)

0

This error is being returned because you are using the object "p" of the first for which it is a float64 object (which is not an eternal object) for the iteration of the second for:

for p in p: 
    for q in p: 

You can use something similar to the code below:

x = 0

while(x < len(p) - 1):
    arrFreq.append(p[x] - p[x + 1])
    x+=1

Where the variable x is used only as a counter, I use a while to traverse to the penultimate element of the array and always compare the element to its successor.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.