Learner Learner - 2 years ago 71
Python Question

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]

Answer Source

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)))

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.


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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download