Write a Python program to find the second most repeated word in a given string.
Answers
Answered by
0
# word in a sequence in Python
from collections import Counter
def secondFrequent(input):
# Convert given list into dictionary
# it's output will be like {'ccc':1,'aaa':3,'bbb':2}
dict = Counter(input)
# Get the list of all values and sort it in ascending order
value = sorted(dict.values(), reverse=True)
# second largest element
secLarge = value[1]
# Traverse dictionary and print key whose value is equal to second large element
for (key, val) in dict.iteritems():
if val == secLarge:
print key
return
# Output
if __name__ == "__main__":
input = ['aaa','bbb','ccc','bbb','aaa','aaa']
secondFrequent(input)
Similar questions