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')
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With