Most used Word in a Sentence – Python Ways
How to find out the most used word in a sentence. This is very useful when you want to analyse a sentence or a text file containing English words. So the problem is
sentance : "this is me and that is my first attempt in python programming" output : is
To solve this problem the approach is something like this
- change the whole sentence into a list
- scan each word from this list
- find out the frequency of this word
- check if the frequency of this word is already present in the dictionary or not
- if the frequency already exist then add the word in the same freqency
- else, add this frequency as a new key and add the word as its value
The solution is now implemented in a python program
str1='this is me and this is in python for class 12 students' list1 = str1.split() freq={} for x in list1: n = list1.count(x) if not n in freq: freq[n] = [x] else: if not x in freq[n]: freq[n] = freq[n]+[x] print(list(freq.items())[:1])
The output of the above code is
rakesh@rakesh:~$ /usr/bin/python3 /home/rakesh/most.py [(2, ['this', 'is'])]