본문 바로가기

Python/Algorithm

[python, algorithm] 가장 흔한 단어 찾기

728x90
반응형

출처: 파이썬 알고리즘 인터뷰

 

문제: 금지된 단어를 제외한 가장 많이 나온 단어를 찾기

 

여기서도 정규식을 활용하여 문제 풀이가 가능하다.

 

str01 = 'aaa bbb dfsdfe ds#@fds ff!@!ffd df 234$# #@$ @$##@@'
print(str01)
print(re.sub(r'[^\w]', ' ', str01))

정규식에서 \w는 단어 문자를 뜻하며 위와 같이 하면 문자가 아니면 전부 공백으로 처리할 수 있다.

 

print 결과 아래와 같이 여러 문자들은 없어진다.

aaa bbb dfsdfe ds#@fds ff!@!ffd df 234$# #@$ @$##@@
aaa bbb dfsdfe ds  fds ff   ffd df 234

 

이외에도 소대문자 통일 및 금지된 단어 제외를 하는 방법을 순서대로 출력하면 아래와 같다.

str01 = 'aaa bbb dfsdfe ds#@fds ff!@!ffd df 234$# #@$ @$##@@'
print(str01)
print(re.sub(r'[^\w]', ' ', str01))
print( [word for word in re.sub(r'[^\w]', ' ', str01).lower().split()] )
print( [word for word in re.sub(r'[^\w]', ' ', str01).lower().split() if word not in 'aaa'] )

print:

aaa bbb dfsdfe ds#@fds ff!@!ffd df 234$# #@$ @$##@@
aaa bbb dfsdfe ds  fds ff   ffd df 234
['aaa', 'bbb', 'dfsdfe', 'ds', 'fds', 'ff', 'ffd', 'df', '234']
['bbb', 'dfsdfe', 'ds', 'fds', 'ff', 'ffd', 'df', '234']

 

 

다음은 collections의 Counter 모듈의 most_common을 사용하여 가장 많은 단어를 추출할 수 있다.

순서대로 과정을 보면 아래 코드와 같습니다.

먼저 단어들을 리스트로 만들고, collections.Counter를 이용해서 각 단어 별 카운팅을 합니다.

most_common(n) 은 가장 많은 값을 n개 return 하므로 1을 주면 가장 많이 나온 값을 return 합니다. 이후 [0][0]의 값을 추출하면 됩니다.

str01 = 'aaa bbb bbb dfsdfe ds#@fds ff!@!ffd df 234$# #@$ @$##@@'
words = [word for word in re.sub(r'[^\w]', ' ', str01).lower().split() if word not in 'aaa']
counts = collections.Counter(words)
print( counts )
print( counts.most_common(1) )
print( counts.most_common(1)[0][0] )

print:

Counter({'bbb': 2, 'dfsdfe': 1, 'ds': 1, 'fds': 1, 'ff': 1, 'ffd': 1, 'df': 1, '234': 1})
[('bbb', 2)]
bbb

 

 

code:

def find_word(papagraph : str, banned : str) -> str:
    words = [word for word in re.sub(r'[^\w]', ' ', papagraph).lower().split() if word not in banned]
    counts = collections.Counter(words)
    return counts.most_common(1)[0][0] 
 
반응형