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]
기억해야할 기법
정규식 다시 확인
\w 같은 표현 기억이 잘 안남
리스트 컴프리헨션 활용
특정 조건의 리스트를 만들고 싶을 때 컴프리헨션을 활용하면 좋을듯
dictionary의 key
dict[0][0] - dictionary의 첫 번째 요소의 key
r-string
raw string
print("hi\n")
# hi
print(r"hi\n")
# hi\n
내가 구현한 코드
import re, collections
def most_common_word(s:str, banned:list[str]) -> str:
s = s.lower()
s = re.sub("[^a-z ]", " ", s)
dict_cnt = collections.Counter(s.split())
for k, v in dict_cnt.most_common():
if k not in banned:
return k