I have a directory of files with a format like test_001_3.wav. The first number is just an ID whereas the second number is an index. I want to loop through these filenames and sum the index for each ID. For example, taking the following data
test_001_0.wav
test_001_1.wav
test_001_2.wav
test_002_0.wav
test_002_1.wav
Returned would be
total for test_001: 3
total for test_002: 2
My current code is below. It correctly sums the indexes however I don't know how to make it work for the entire array of filenames.
samples = []
for filename in filenames:
file_id = filename.split('_')[1].split('.')[0]
index = filename.split('_')[-1].split('.')[0]
samples.append([file_id, index])
count=0
for sample in samples:
if sample[0] == '001':
count+=1
print(samples) # [['001', '0'], ['001', '1'], ['001', '2'], ['002', '0'], ['002', '1']]
print(count) # 3