Learner
Learner

Reputation: 53

Getting key values from a list of dictionaries

I have a list that contains dictionaries with Letters and Frequencies. Basically, I have 53 dictionaries each for every alphabet (lowercase and uppercase) and space.

adict = {'Letter':'a', 'Frequency':0}
bdict = {'Letter':'b', 'Frequency':0}
cdict = {'Letter':'c', 'Frequency':0}

If you input a word, it will scan the word and update the frequency for its corresponding letter.

for ex in range(0, len(temp)):
    if temp[count] == 'a': adict['Frequency']+=1
    elif temp[count] == 'b': bdict['Frequency']+=1
    elif temp[count] == 'c': cdict['Frequency']+=1

For example, I enter the word "Hello", The letters H,e,l,l,o is detected and its frequencies updated. Non zero frequencies will be transferred to a new list.

if adict['Frequency'] != 0 : newArr.append(adict) 
if bdict['Frequency'] != 0 : newArr.append(bdict)
if cdict['Frequency'] != 0 : newArr.append(cdict) 

After this, I had the newArr sorted by Frequency and transferred to a new list called finalArr. Below is a sample list contents for the word "Hello"

{'Letter': 'H', 'Frequency': 1}
{'Letter': 'e', 'Frequency': 1}
{'Letter': 'o', 'Frequency': 1}
{'Letter': 'l', 'Frequency': 2}

Now what I want is to transfer only the key values to 2 seperate lists; letterArr and numArr. How do I do this? my desired output is:

letterArr = [H,e,o,l]
numArr = [1,1,1,2]

Upvotes: 3

Views: 96

Answers (3)

Marichyasana
Marichyasana

Reputation: 3154

You wanted a simple answer without further todo like zip, collections, itemgetter etc. This does the minimum to get it done, 3 lines in a loop.

finalArr= [{'Letter': 'H', 'Frequency': 1},
           {'Letter': 'e', 'Frequency': 1},
           {'Letter': 'o', 'Frequency': 1},
           {'Letter': 'l', 'Frequency': 2}]

letterArr = []
numArr    = []
for i in range(len(finalArr)):
    letterArr.append(finalArr[i]['Letter'])
    numArr.append(finalArr[i]['Frequency'])
print letterArr
print numArr

Output is

['H', 'e', 'o', 'l']
[1, 1, 1, 2]

Upvotes: 0

mhawke
mhawke

Reputation: 87074

Why don't you just use a collections.Counter? For example:

from collections import Counter
from operator import itemgetter

word = input('Enter a word: ')
c = Counter(word)

letter_arr, num_arr = zip(*sorted(c.items(), key=itemgetter(1,0)))
print(letter_arr)
print(num_arr)

Note the use of sorted() to sort by increasing frequency. itemgetter() is used to reverse the sort order so that the sort is performed first on the frequency, and then on the letter. The sorted frequencies are then separated using zip() on the unpacked list.

Demo

Enter a word: Hello
('H', 'e', 'o', 'l')
(1, 1, 1, 2)

The results are tuples, but you can convert to lists if you want with list(letter_arr) and list(num_arr).

Upvotes: 3

Adrien El Zein
Adrien El Zein

Reputation: 179

I have a hard time understanding your data structure choice for this problem. Why don't you just go with a dictionary like this:

frequencies = { 'H': 1, 'e': 1, 'l': 2, 'o': 1 }

Which is even easier to implement with a Counter:

from collections import Counter
frequencies = Counter("Hello")
print(frequencies)
>>> Counter({ 'H': 1, 'e': 1, 'l': 2, 'o': 1 })

Then to add another word, you'd simply have to use the updatemethod:

frequencies.update("How")
print(frequencies)
>>> Counter({'l': 2, 'H': 2, 'o': 2, 'w': 1, 'e': 1})

Finally, to get your 2 arrays, you can do:

letterArr, numArr = zip(*frequencies.items())

This will give you tuples though, if you really need lists, just do: list(letterArr)

Upvotes: 1

Related Questions