为了忘记,写一遍就练习一遍~
import numpy as np
def most_frequent(s):
word_list = s.split(' ')
print(word_list)
new_list = []
count = []
for w in word_list:
if w not in new_list:
new_list.append(w)
print(new_list)
new_list = np.sort(new_list)
print(new_list)
for w in new_list:
count.append(word_list.count(w))
print(count)
inx = count.index(max(count))
print(new_list[inx])
return new_list[inx]
s = 'I scream you scream we all scream for ice cream'
most_frequent(s)
s = 'I love you !'
most_frequent(s)
没有添加注释,请谅解~