819. Most Common Word
私の答え:
class Solution:
def mostCommonWord(self, paragraph, banned):
# 일단 잡 문자들 다 공백으로 치환
for char in paragraph:
if char.isalpha() == False:
paragraph = paragraph.replace(char, ' ') # replace() Return a copy of the string
paragraph = paragraph.lower() # 소문자로 변경
words = paragraph.split() # 단어들 포함한 리스트 생성
removed = []
for word in words:
if word not in banned:
removed.append(word)
dict = collections.Counter(removed) # counter 객체는 아이템에 대한 개수를 계산해 딕셔너리로 리턴함
pre_results = dict.most_common(1) # 가장 많이 있는거 1개
return pre_results[0][0]
入力値paragraph = "Bob. hIt, baLl"
banned = ["bob", "hit"]
最初はこのようにしていましたが、このようにすると、上のような値が入ってきたときに、最初のbob
が単語から削除され、単語の中に['hit', 'ball']
が残っていましたが、ドアに座って単語の最初にアクセスしたので、次の単語はball
で、hit
は削除できないので間違っていました.for word in words:
if word in banned:
words.remove(word)
したがって、もう1つのリストが作成され、word
がbanned
に含まれていない場合、それをリストに入れ、リストに基づいて複数の単語を検索して解決する. removed = []
for word in words:
if word not in banned:
removed.append(word)
本の正解:class Solution:
def mostCommonWord(self, paragraph, banned):
# \w - 단어 문자(word character)를 뜻하며 문자+숫자(alphanumeric)와 매치, [a-zA-Z0-9_]와 동일한 표현식임.
# ^는 not을 의미함.
# re.sub(pattern, repl, string, count=0, flags=0)
# pattern은 정규식이고, repl은 바꿀것, pattern에 해당하는게 안보이면 string이 리턴됨.
# 따라서 re.sub를 사용해서 정규식으로 단어가 아닌것을 공백으로 바꿔줌.
# 이제 공백이 아닌 단어들이 나오는데 여기서 전부 소문자로 바꾸고 공백 단위로 단어들을 나눈후에 banned에 없는 단어들인 word로 리스트 생성함.
# 리스트 컴프리헨션 https://wikidocs.net/22805
words = [word for word in re.sub(r'[^\w]', ' ', paragraph)
.lower().split()
if word not in banned]
# 가장 흔하게 등장하는 단어의 첫 번째 인덱스 리턴
counts = collections.Counter(words)
# most_common([n])
# Return a list of the n most common elements and their counts from the most common to the least.
# If n is omitted or None, most_common() returns all elements in the counter.
# Elements with equal counts are ordered in the order first encountered:
return counts.most_common(1)[0][0]
most_commonReference
この問題について(819. Most Common Word), 我々は、より多くの情報をここで見つけました https://velog.io/@oem0404/819.-Most-Common-Wordテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol