Search code examples
pythonlistpython-3.xfor-loopenumerate

Indexing through a list in python


I have a list that I'm trying to loop through and index the number of each number in the list. The total list is around 1500-2000 numbers where each number represents subject behavior. I imported the list of numbers via an excel sheet:

import openpyxl
from openpyxl import load_workbook
wb = load_workbook('/data.xlsx')
ws = wb['bhv.TrialError'] 
trialerror10_22=[]
for column in ws.columns:
    for cell in column:
        trialerror10_22=(cell.value)
        print(trialerror10_22)

This all works fine and well and prints out my 1500 or so numbers in a list. Next I try to index the numbers, which range from 0 to 9 and are associated with the labels in this order:

labels = ['C','NR', 'LR', 'BF', 'NF', 'ER', 'IR', 'LB', 'I', 'A']

def bhvread(trialerror10_22):
    test=False
    results = [0] * (max(data) + 1)
    for val in data:
        results[val] +=1
    return results


for index, val in enumerate(results):
    print(index, labels[index], val)

But when I run this part it says Index Error: list index out of range and points to the last line of my code.

Output:

0 C 1
1 NR 0
2 LR 0
3 BF 0
4 NF 0
5 ER 0
6 IR 0
7 LB 0
8 I 0
9 A 0

Solution

  • Update 2:

    If I understand you correctly you want to do something like:

    data = [0, 1, 2, 0, 1, 0, 9, 8]
    #dictionary keeping track of our number counters
    results = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}
    labels = ['C', 'NR', 'LR', 'BF', 'NF', 'ER', 'IR', 'LB', 'I', 'A']
    
    for x in data:
        #is the data valid?
        if x >= 0 and x < 10:
            results[x] += 1 #increment the correct entry in the dict
    
    #print everything
    for i in range(10):
        print(i, labels[i], results[i])
    

    output

    0 C 3
    1 NR 2
    2 LR 1
    3 BF 0
    4 NF 0
    5 ER 0
    6 IR 0
    7 LB 0
    8 I 1
    9 A 1
    

    Original:

    The problem is definitely with labels[index].

    If len(results) is greater than 10 you will run into troubles on the tenth iteration.

    Example:

    results = [1,2,3,4,5,6,7,8,9,10,11]
    #len(results) = 11
    
    #first run
    index    v
    results [1   , 2  , 3  , 4   , 5   , 6   , 7   , 8   , 9  , 10   , 11]
    labels  ['C','NR', 'LR', 'BF', 'NF', 'ER', 'IR', 'LB', 'I', 'A']
    
    #second run
    index          v
    results [1   , 2  , 3  , 4   , 5   , 6   , 7   , 8   , 9  , 10   , 11]
    labels  ['C','NR', 'LR', 'BF', 'NF', 'ER', 'IR', 'LB', 'I', 'A']
    
    #More runs
    
    #tenth run
    index                                                       v
    results [ 1  , 2  , 3  , 4   , 5   , 6   , 7   , 8   , 9  , 10   , 11]
    labels  ['C','NR', 'LR', 'BF', 'NF', 'ER', 'IR', 'LB', 'I', 'A']
    
    #eleventh run
    index                                                              v
    results [ 1  , 2  , 3  , 4   , 5   , 6   , 7   , 8   , 9  , 10   , 11]
    labels  ['C','NR', 'LR', 'BF', 'NF', 'ER', 'IR', 'LB', 'I', 'A'] #INDEX ERROR!!!
    

    It is pretty unclear what you try to accomplish but I hope this gets you on the right track.

    Update:

    As @Elzell said you may want to use itertools.cycle

    import itertools
    #...
    labels_iterator = itertools.cycle(labels)
    
    for index, val in enumerate(results):
        print(index, labels_iterator.next() , val)