Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- class Solution():
- def topNBuzzWords(self,numToys, topToys,toys,numQuotes,quotes):
- cnt = { toy: [0,0] for toy in toys}
- for quote in quotes:
- is_updated = {toy: False for toy in toys}
- for word in quote.lower().split():
- word = re.sub('[^a-z]','',word)
- if word in toys:
- cnt[word][0]+=1
- is_updated[word] = True
- for key in is_updated.keys():
- if is_updated[key]==True:
- cnt[key][1] += 1
- return [toy for toy, freq in sorted(cnt.items(), key=lambda item:(item[1][0],item[1][1]), reverse=True)][:topToys]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement