Dale
Dale

Reputation: 15

comparing a dictionary containing lists

I have a dictionary of lists which contains 26 keys, each of which has 26 list of a given length. These keys represent the letters of the English Latin alphabet. The list contain occurrences of a given character in a given spot for words of a certain length. For example if we want to represent the occurrence of words of length 5, I may receive the following output:

D = {'a': [5, 2, 0, 1, 4], …., 'z': [0, 7, 5, 2, 1]}

My goal is to compare key a to key z by index. So I want to compare 'a':[5] to 'z':[0] and if 'a' > 'z' then I want to return a. Essential I want to compare each index and if that index is larger, I want to return the letter for that index. My current code is as follows:

def most_common_character_by_index(D):

    for key in D:
        for value in key:
            f = map(D[key], D[value] )
    print(list[f])

The thought was to map the indices and compare each. Perhaps I'm missing something? The current error code returns: print(list[f]) TypeError: 'type' object is not subscriptable

Upvotes: 0

Views: 64

Answers (2)

Marcos Modenesi
Marcos Modenesi

Reputation: 277

The main problem is this:

list[f]

you are putting brackets which means you are trying to subscritp the list constructor. Hence the error.

What you want is

list(f)

Anyway, there are other problems:

for value in key: will iterate over the key itself, you may want for value in D[key].

Also, f is used out of the loop.

Upvotes: 0

Sam
Sam

Reputation: 147

def mostCommonLetters(D):

    numberOfValues = len(D['a'])

    listOfMostCommonLetters = []

    for i in range(numberOfValues):
        currMax = D['a'][i]
        mostCommonLetter = 'a'
        for letter in D:
            if D[letter][i] >= currMax:
                currMax = D[letter][i]
                mostCommonLetter = letter
        listOfMostCommonLetters.append(mostCommonLetter)

    return listOfMostCommonLetters

print(mostCommonLetters({'a': [5, 2, 0, 1, 4],'z': [0, 7, 5, 2, 1]}))

Upvotes: 1

Related Questions