Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return output of dictionary to alphabetical order

The following code prints out the word in the txt file and then how many instances there are of that word (e.g. a, 26) the problem is that it doesn't print it out in alphabetical order. Any help would be much appreciated

import re
def print_word_counts(filename):
    s=open(filename).read()
    words=re.findall('[a-zA-Z]+', s)
    e=[x.lower() for x in (words)]
    e.sort()
    from collections import Counter
    dic=Counter(e)
    for key,value in dic.items():
        print (key,value)
print_word_counts('engltreaty.txt')
like image 744
user2101517 Avatar asked May 17 '13 01:05

user2101517


People also ask

Are dictionaries in alphabetical order?

A dictionary is a resource that lists the words of a language (typically in alphabetical order) and gives their meaning. It can often provide information about pronunciation, origin, and usage.


1 Answers

You just need to sort the items. The builtin sorted should work wonderfully:

for key,value in sorted(dic.items()):
    ...

If you drop the e.sort() line, then this should run in approximately the same amount of time. The reason that it doesn't work is because dictionaries are based on hash tables which store items in order of their hash values (with some more complicated stuff when hash collisions occur). Since the hashing function is never specified anywhere, it means that you can't count on a dictionary keeping any order that you try to give it and that the order is implementation and version dependent. For other simple cases, the collections module has an OrderedDict subclass which does keep insertion order. however, that won't really help you here.

like image 143
mgilson Avatar answered Sep 24 '22 01:09

mgilson