As the title says:
So far this is where I\'m at my code does work however I am having trouble displaying the information in order. Currently it just displays the inf
Dictionaries are unordered data structures. Also if you want to count some items within a set of data you better to use collections.Counter() which is more optimized and pythonic for this aim.
Then you can just use Counter.most_common(N) in order to print most N common items within your Counter object.
Also regarding the opening of files, you can simply use the with statement that closes the file at the end of the block automatically. And it's better to not print the final result inside your function instead, you can make your function a generator by yielding the intended lines and then printing them when even you want.
from collections import Counter
def frequencies(filename, top_n):
with open(filename) as infile:
content = infile.read()
invalid = "‘'`,.?!:;-_\n—' '"
counter = Counter(filter(lambda x: not invalid.__contains__(x), content))
for letter, count in counter.most_common(top_n):
yield '{:8} appears {} times.'.format(letter, count)
Then use a for loop in order to iterate over the generator function:
for line in frequencies(filename, 100):
print(line)