Home > Back-end >  How should I put numpy in the list?
How should I put numpy in the list?

Time:08-10

I'm a student and I'm going to divide the wave file into 5 seconds and preprocess it with MFCC, Python

This is the full np.array of the file. And np.array divided by 5 seconds.

print(audios[0].shape)
print(audios[0][0:int(sr*5)].shape

(9091892,)
(220500,)

So I wrote the following code to divide it into 5 seconds with numpy.

audios_five = []
for i in range(len(audios)):#0~23
    print("audios" str(i 1) ".wav")
    for j in range(len((audios[i])//int(sr*5)) 1):
        audios_five[i].append((audios[i][int(sr*5)*j:int(sr*5)*(j 1)]).tolist())
        print("audios_five" str(i 1) "_" str(j 1) ".wav")
        print(audios_five[i][j])

And that's the result.

enter image description here

I ask for your help me. I have no idea what the problem is...

CodePudding user response:

The problem

The problem is that the 'list index is out of range', meaning that i is not a valid index in audios_five. This makes sense, given that audios_five is created as an empty list at the start of the code snippet. If you run the following (simplified) code, you get the same error:

test = []
test[0].append('something')

Yields

----> 1 test[0].append('something')
IndexError: list index out of range

The solution

For ease of notation, lets refer to (audios[i][int(sr*5)*j:int(sr*5)*(j 1)]).tolist() as x.

What is necessary depends on what you want to achieve.

Option 1: Create a nested list

I'm not much for dealing with audio in Python, but what your currently trying to do is append something to the first value in audios_five - i.e. create a nested list. If this is what you want, you first need an audios_five.append(#create some item) before you can append to this item with audio_five[0].append.

The result would be something like

audios_five = [[x1, x2, x3], [x4, x5, x6], ...]

Option 2: Create a flat list

However, if you want a flat list, so you just want to append x to the audios_five, then you would be looking at using audios_five.append(x).

The result would then be something like

audios_five = [x1, x2, x3, x4, ...]

  • Related