I have this program that reads a file and prints the desired amount of most common words. I don't know how to print the words that appear the same amount of times.
Here's my code:
number_of_words = int(input('Enter how many top words you want to see: '))
uniques = []
stop_words = ["a", "an", "and", "in", "is"]
for word in words:
check_special = False
if word.isalnum():
check_special = True
if word not in uniques and word not in stop_words and check_special:
uniques.append(word)
counts = []
for unique in uniques:
count = 0
for word in words:
if word == unique:
count = 1
counts.append((count, unique))
counts.sort()
counts.reverse()
for i in range(min(number_of_words, len(counts))):
count, word = counts[i]
print('The following words appeared %d each: %s ' % (count, word))
As a demo it prints:
The following words appeared 11 each: night
The following words appeared 11 each: go
I want the output to be:
The following words appeared 11 each: go, night
How can I achieve this?
CodePudding user response:
We can achieve by using this.
count_with_word = {}
for i in range(min(number_of_words, len(counts))):
count, word = counts[i]
if count in count_with_word:
count_with_word[count].append(word)
else:
count_with_word[count] = [word]
for count, words in count_with_word.items():
print('The following words appeared %d each: %s ' % (count, ', '.join(words)))
Output would be like:
The following words appeared 2 each: t1, t2
CodePudding user response:
Use dict
counts_dict = {count: [] for count, word in counts}
for count, word in counts:
counts_dict[count].append(word)
count_num_word = 0
for count in counts_dict:
if count_num_word >= number_of_words:
break
print('The following words appeared %d each: %s ' % (count, ', '.join(counts_dict[count])))
count_num_word = len(counts_dict[count])
e.g.
words = ['B', 'B', 'A', 'A', 'C']
> Enter how many top words you want to see: 3
> The following words appeared 2 each: B, A
> The following words appeared 1 each: C
> Enter how many top words you want to see: 2
> The following words appeared 2 each: B, A
> Enter how many top words you want to see: 1
> The following words appeared 2 each: B, A
Although you input number_of_words
as 1, it should return all words that share the same count, as shown in the example.