Python中的spilt方法只能通过指定的某个字符分割字符串,如果需要指定多个字符,需要用到re模块里的split方法。
正则介绍:https://www.cnblogs.com/greatfish/p/7572131.html
>>> import re
>>> a = "Hello world!How are you?My friend.Tom"
>>> re.split(" |!|\?|\.", a)
['Hello', 'world', 'How', 'are', 'you', 'My', 'friend', 'Tom']
import re
re.split('_#|','this_is#a|test')
class Solution:
def mostCommonWord(self, paragraph, banned):
"""
:type paragraph: str
:type banned: List[str]
:rtype: str
"""
# Approach #1
# 1、转换大写字母为小写。
# 2、按照空格切分单词。
# 3、去除标点以及在banned中的词,构造新的列表。
# 4、用Counter()函数构造字典。
# 5、字典反转。
# 6、选取key最大的value。
from collections import Counter
Adic = {v:k for k,v in Counter([i.strip('!?\',;.') for i in paragraph.lower().split(' ') if i.strip('!?\',;.') not in banned] ).items()}
return Adic[max(Adic.keys())]
我的,ide可以,leetcode gg了。
import re
class Solution(object):
def mostCommonWord(self, paragraph, banned):
"""
:type paragraph: str
:type banned: List[str]
:rtype: str
"""
# a = 'one1two2three3four4five5'
# print(re.split('\d+', a))
# para = paragraph.split(",")
# print(para)
para = re.split(" |,|\.|\s+",paragraph.lower())
# print(para)
ban = "".join(banned)
# print(ban)
dict = {}
# print(ban)
# assert ban==
for i in para:
# print(i)
if i==ban:
continue
if i not in dict.keys():
dict[i] = 1
else:
count = dict[i]
count += 1
dict[i]=count
# print(dict)
maxlen = max(i for i in list(dict.values()) if i!='')
for key,value in dict.items():
if value == maxlen:
return str(key)