2

I will try to describe the core of my problem: I have a list of signals, each with its own alias, and a dataset of many entries of alias, value, and the quality of the value (the qualitycan be 0, 1, or 2). i am trying to loop through the signals, find its alias in the dataset, and update a counter of how many times each signals has been written with a given quality. I have built a dictionary

dati = {'signals': [signal1, signal2, ...], 'alias': [alias1, alias2, ...], 'nGOOD': [0, 0, ...], 'nBAD': [0, 0, ...], 'nFREEZE': [0, 0, ...]}

and I ended up writing the code like this:

for i in range(0, len(dati['signals'])):

    #I extract the "quality" information using the alias
    quality = extracted using(dati['alias'][i])
    ######
    
    if quality == '0':
        dati['nGOOD'][i] += 1
    elif quality == '1':
        dati['nBAD'][i] += 1
    elif quality == '2':
        dati['nFREEZE'][i] += 1
    else:
        raise Exception('Qualità sconosciuta')

This code worked, but I understand that using indexes is not the most pythonic why of doing stuff. I know I can use zip() to loop through different lists, but then I get tuples, which cannot be modified. Is there a better way to do this?

Thank you for your time.

@Wups: the final output of the code is the signal name and the quality counts, so I need to keep the informations linked.

@Pranav Hosangadi: if I understand correctly, using enumerate would be somewhat cleaner, but it is still not what I hoped for. I would still have to modify the quality by calling them by index. I had hoped for a way to access them during the loop.

Topomouse
  • 23
  • 6

1 Answers1

1

You can use enumerate() to avoid having to index the alias list. But you still need to use indexes for the values to increment.

for i, alias in enumerate(dati['alias']):
    quality = extract quality from alias

    if quality == '0':
        dati['nGOOD'][i] += 1
    elif quality == '1':
        dati['nBAD'][i] += 1
    elif quality == '2':
        dati['nFREEZE'][i] += 1
    else:
        raise Exception('Qualità sconosciuta')

Things would be easier if you had a list of dictionaries.

dati = {
    {'signal': signal1, 'alias': alias1, 'nGOOD': 0, 'nBAD': 0, 'nFREEZE': 0},
    {'signal': signal2, 'alias': alias2, 'nGOOD': 0, 'nBAD': 0, 'nFREEZE': 0},
    ...
}
for datum in dati:
    quality = extract quality from datum['alias']
    if quality == '0':
        datum['nGOOD'] += 1
    elif quality == '1':
        datum['nBAD'] += 1
    elif quality == '2':
        datum['nFREEZE'] += 1
    else:
        raise Exception('Qualità sconosciuta')

Generally, keeping related attributes together simplifies things over having a separate array for each attribute.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • This could work. I had the data in separate arrays because it had been useful while I was building them, but nothing stops me from switching its format. I do not know on the top of my head if I can easily switch between a disctionary of lists and a list of disctionaries, but It can basically do the same with a list of lists and "transpose" it using dati= list(zip(*dati)). – Topomouse Sep 17 '20 at 09:37