简体   繁体   中英

A more pythonic way of looping through multiple lists while modifying some of them

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.

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM