简体   繁体   English

在修改其中一些列表的同时循环遍历多个列表的更 Pythonic 的方式

[英]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).我将尝试描述我的问题的核心:我有一个信号列表,每个信号都有自己的别名,以及一个包含别名、值和值质量(质量可以是 0、1 或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.这段代码有效,但我明白使用索引并不是最 Pythonic 的原因。 I know I can use zip() to loop through different lists, but then I get tuples, which cannot be modified.我知道我可以使用 zip() 来遍历不同的列表,但是我得到了无法修改的元组。 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. @Wups:代码的最终输出是信号名称和质量计数,所以我需要保持信息链接。

@Pranav Hosangadi: if I understand correctly, using enumerate would be somewhat cleaner, but it is still not what I hoped for. @Pranav Hosangadi:如果我理解正确,使用 enumerate 会更简洁一些,但这仍然不是我所希望的。 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.您可以使用enumerate()来避免索引alias列表。 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.通常,将相关属性放在一起可以简化为每个属性设置单独数组的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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