1. 程式人生 > >python正則---819. 最常見的單詞

python正則---819. 最常見的單詞

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)