문장이 주어지고 금지된 단어들을 제외하고 가장 많이 등장한 단어을 리턴해라!
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
"ball"
class Solution:
def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
# 전처리
words = [word for word in re.sub(r'[^\w]', ' ', paragraph).lower().split()
if word not in banned]
# 딕셔너리
counts = collections.Counter(words)
return counts.most_common(1)[0][0]
전처리는 책의 풀이를 가져다 사용
class Solution:
def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
words = [word for word in re.sub(r'[^\w]', ' ', paragraph).lower().split()
if word not in banned]
count_dict = {}
for word in new_words:
if word in count_dict.keys():
count_dict[word] += 1
else:
count_dict[word] = 1
return(max(count_dict, key = lambda x: count_dict[x]))