Determining the most common word from a user's input. [Python]
count, list, python, string
Solution
Use a `collections.Counter` class. I'll give you a hint.
>>> from collections import Counter
>>> a = Counter()
>>> a['word'] += 1
>>> a['word'] += 1
>>> a['test'] += 1
>>> a.most_common()
[('word', 2), ('test', 1)]
You can extract the word and the frequencies from here.
Using it to extract frequencies from user input.
>>> userInput = raw_input("Enter Something: ")
Enter Something: abc def ghi abc abc abc ghi
>>> testDict = Counter(userInput.split(" "))
>>> testDict.most_common()
[('abc', 4), ('ghi', 2), ('def', 1)]
Problem
The way I tried to solve this problem was by entering the words of a user into a list and then using .count() to see how many times the word is in the list. The problem is whenever there is a tie, I need to print all of the words that appear the most amount of times. It works only if the words that I use aren't inside of another word that appears the same amount of times. Ex: if I use Jimmy and Jim in that order, it will only print Jimmy. ``` for value in usrinput: dict.append(value) for val in range(len(dict)): count = dict.count(dict[val]) print(dict[val],count) if (count > max): max = count common= dict[val] elif(count == max): if(dict[val] in common): pass else: common+= "| " + dict[val] ```