3

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] 
4
  • 3
    You have three separate dictionaries. They're not associated in any way. That means there's no easy way to iterate through them. Why do each of your dictionaries only contain two elements? Why not use one dictionary, using the letter as the key and the frequency as the value? Commented May 7, 2016 at 12:06
  • 1
    Why do you have 53 dictionaries in the first place? You could do it like freq_dict = {'a': 0, 'b': 0, ...}. Then it would be easy to transfer frequencies into a list. Commented May 7, 2016 at 12:18
  • Just makes it easier for me to understand. Basically my final list is just a list of dictionaries. Commented May 7, 2016 at 12:23
  • It's easier to work with a more complicated and unfit approach first? Well, if you say so ... Commented May 7, 2016 at 12:46

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Haven't thought of this before. You sir have my big thanks! Just made my code shorter. Still need to learn alot of things in python.
1

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)

Comments

0

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] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.