I want to create a new array that has some sifted elements from an old array
This is what the array looks like:
print(freqs[0])
print(freqs[1])
print(freqs[2])
print(freqs[3])
print(freqs[4])
>>(-1.356524149576826+0j)
(-1.5188337718697111-0.0028277787696928914j)
(-1.537925820349816+0.02545857328595845j)
(-1.7483732926022144+0.0875845050778933j)
(-1.8506826888571075+0.16201185108446528j)
when I cast the index "manually" to troubleshoot it all works fine
print(int(0*scaler), freqs[int(0*scaler)])
print(int(1*scaler), freqs[int(1*scaler)])
print(int(2*scaler), freqs[int(2*scaler)])
print(int(3*scaler), freqs[int(3*scaler)])
print(int(4*scaler), freqs[int(4*scaler)])
>>0 (-1.356524149576826+0j)
0 (-1.356524149576826+0j)
1 (-1.5188337718697111-0.0028277787696928914j)
2 (-1.537925820349816+0.02545857328595845j)
3 (-1.7483732926022144+0.0875845050778933j)
However, once I put this kind of logic in a loop I always get the 0th element. And I doubled checked that my index variable is in fact an int
for i in range(length):
index = int(i*scaler)
if index < length:
newfreqs[i] = freqs[index]
print(i,index,freqs[index])
>>0 0 (-1.356524149576826+0j)
1 0 (-1.356524149576826+0j)
2 1 (-1.356524149576826+0j)
3 2 (-1.356524149576826+0j)
4 3 (-1.356524149576826+0j)
I get the exact same behavior if I use math.floor() instead of int(). Oddly round() seems to work fine but it does not produce the correct index value I need. It is only an issue anywhere inside the loop, outside the loop it works as expected.
Here is the entire function
def reindex(freqs, halfSteps):
scaler = halfStep**(-halfSteps)
newfreqs = freqs
length = len(newfreqs)
for i in range(length):
index = int(i*scaler)
if index < length:
newfreqs[i] = freqs[index]
print(i,index,freqs[index])
return newfreqs
Changed the initialization of newfreq to 'zeros(len(freqs))'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.