KDE
KDE

Reputation: 85

Can't get my head around the problem, list index out of range (inside 3 loops)

I know list index out of range has been covered a million times before and I know the issue is probably that I am trying to reference an index position that does not exist but as there are 3 for loops nested I just cant figure out what is going on.

I am trying to calculate the frequency of each letter of the alphabet in a list of words.

alphabet_string = string.ascii_uppercase
g = list(alphabet_string)
a_count = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
y = 0

for word in words:
    for chars in word:
        for letters in chars:
            if letters == g[y]:
                a_count[y] = a_count[y] +1
        y = y + 1
print(a_count[0])

word is in the format of: ['ZYMIC']

chars is in the format of: ZYMIC

letters is in the format of: C

If I substitute the y value for a value between 0 and 25 then it returns as expected. I have a feeling the issue is as stated above that I am exceeding the index number of 25, so I guess y = y + 1 is in the wrong position. I have however tried it in different positions.

Any help would be appreciated.

Thanks!

Edit: Thanks everyone so much, never had this many responses before, all very helpful!

Upvotes: 0

Views: 57

Answers (3)

Nk03
Nk03

Reputation: 14949

Solution via Counter -

from collections import Counter
words = ['TEST','ZYMIC']
print(Counter(''.join(words)))

If you wanna stick to your code then change the if condition -

when y = 0 g[y] means 'A' and you're checking if 'A' == 'Z' which is the 1st letter. Basically, you need to fetch the index location of the element from list g and increase the value by 1. That's what you need to do to make it work. If I understood your problem correctly.

import string
words = ['ZYMIC']
alphabet_string = string.ascii_uppercase
g = list(alphabet_string)
a_count = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

for word in words:
    for chars in word:
        for letters in chars:
            if letters in g:
                y = g.index(letters)
                a_count[y] += 1
print(a_count)

And you can very well replace the if condition, and check for the index directly because the letter will always be there in g. Therefore, this particular condition is redundant here.

for word in words:
    for chars in word:
        for letters in chars:
            y = g.index(letters)
            a_count[y] += 1

Upvotes: 1

Gusti Adli
Gusti Adli

Reputation: 1213

Storing a_count as a dictionary is the better option for this problem.

a_count = {}
for word in words:
    for chars in word:
        for letters in chars:
            a_count[letters] = a_count.get(letters, 0) + 1

You can also use the Counter() class from the collections library.

from collections import Counter

a_count = Counter()
for word in words:
    for chars in word:
        for letters in chars:
            a_count[letters] += 1

print(a.most_common())

Upvotes: 1

Vinz
Vinz

Reputation: 21

I think it's because of list a_count. I would suggest another approach here, based on dictionaries:

listeletters = ['foihroh','ZYMIC','ajnaisui', 'fjindsonosn']

alphabeth = {'a' : 0, 
        'b' : 0,
        'c': 0}

for string in listeletters:
    for l in string:
        if l in alphabeth.keys():
            alphabeth[l] = alphabeth[l] + 1

print(alphabeth)

I inialize the alphabeth and then I get the result wanted

Upvotes: 0

Related Questions