如何在“ for”循环中增加特定变量?

时间:2019-01-30 13:55:58

标签: python arrays loops

我有567个数组,每个数组都有特定的长度。我需要逐一检查这567个数组中的每个元素,并根据找到的元素,我需要增加一个 specific 变量。

分别创建了100个计数器,所有计数器均以0开头,分别名为counter_0,counter_1 ...,counter_99。

当我检查每个数组的每个值时,我需要增加与数组值相对应的计数器,这意味着,如果我在数组1的位置1找到值 90 ,我必须递增counter_90。 567个数组的长度各不相同,但是所有包含的值从0到99 ...

如何在for循环中执行整个操作,并引用变量?

我尝试过:

for i in range(567):
    #this is a way of refer to array_i:
    for j in range(len('array_{}'.format(i))):
         #check each position of this array
            #if position checked has value = 10, increment counter_10
            #if position checked has value = 22, increment counter_22
               ...

我不知道该怎么办。

1 个答案:

答案 0 :(得分:1)

最好列出100个数字,

counters = [0] * 100

并将索引存储在字典中

update = { 10: 10, 22 : 22 }

那你可以说

counters[update[checked_value]]+=1