class Solution:
def mostCommonWord(self, paragraph, banned):
import re
res = paragraph#中间变量
banned=sorted(banned,key=len,reverse=True)#根据修改的长度进行排序
if banned:
while banned:
pattern = '{}'.format(banned[0])
res = re.sub(pattern,"",res,flags=re.IGNORECASE)
banned.pop(0)
word_list = re.findall("[a-zA-Z]+",res)
dic = {}
for word in [i.lower() for i in word_list]:
dic.setdefault(word,0)
dic[word]+=1
res = sorted(dic.items(),key=lambda item:item[1],reverse=True)
return res[0][0]
05-13
534
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-18
595
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交