#第一种:利用正则切割
import re
from collections import Counter
def max_count_word(s):
a = re.compile(r'\W').split(s)
lt = list(a)
c = Counter(lt)
d2 = dict(c)
lt = []
d1 = {}
for i in d2:
s, n = i, d2[i]
d1[d2[i]] = i
lt = list(d1)
lt.sort(reverse=True)
return (d1[lt[1]])
#测试
s = 'i love you and i love he love me,you may be is fool'
print(max_count_word(s))
#第二种,根据空格进行切割
def max_count_word(s):
d = 0
lt = []
for i in range(len(s) - 1):
if not s[i].isalpha():
if i > d:
lt.append(s[d:i])
d = i + 1
if s[-1].isalpha():
lt.append(s[d:])
for s in range(len(lt) - 1):
for j in range(len(lt) - s - 1):
if (lt.count(lt[j]) < lt.count(lt[j + 1])):
lt[j], lt[j + 1] = lt[j + 1], lt[j]
return lt[len(lt[0])]
1.返回一个字符串中出现次数第二多的单词 2.字符串中可能有英文单词、标点、空格 3.字符串中的英文字符全部是小写
猜你喜欢
转载自blog.csdn.net/LoveL_T/article/details/81586850
今日推荐
周排行