lines = Counter({'you': 29, 'i': 24, 'my': 17, 'more': 13, 'one': 12, 'night': 12, 'go': 11, 'yeah': 10})
my code is this:
word = Counter(lines)
from collections import defaultdict
reversed_word = defaultdict(list)
for value, count in word.most_common(top_words):
reversed_word[count].append(value)
for key in sorted(reversed_word, reverse= True):
print("The following words appeared {} times each: {}".format(key,', '.join(sorted(reversed_word[key]))))
if top_words = 5
output:
The following words appeared 29 times each: you
The following words appeared 24 times each: i
The following words appeared 17 times each: my
The following words appeared 13 times each: more
The following words appeared 12 times each: night, one
but with my code i get:
The following words appeared 29 times each: you
The following words appeared 24 times each: i
The following words appeared 17 times each: my
The following words appeared 13 times each: more
The following words appeared 12 times each: night
i get the output I need if I change top_words to 6:
The following words appeared 29 times each: you
The following words appeared 24 times each: i
The following words appeared 17 times each: my
The following words appeared 13 times each: more
The following words appeared 12 times each: night, one
how do I fix this?