[프로그래머스] 뉴스 클러스터링- 파이썬
https://programmers.co.kr/learn/courses/30/lessons/17677?language=python3
코딩테스트 연습 - [1차] 뉴스 클러스터링
뉴스 클러스터링 여러 언론사에서 쏟아지는 뉴스, 특히 속보성 뉴스를 보면 비슷비슷한 제목의 기사가 많아 정작 필요한 기사를 찾기가 어렵다. Daum 뉴스의 개발 업무를 맡게 된 신입사원 튜브
programmers.co.kr
문제를 풀다가 뭔가 불편했던...?문제이다?
이걸 어케 풀지?
뭐 내가 모르는 파이썬의 제공함수들이 있나?
사람들은 왜 풀만하다고하지?
이랬는데 아니나 다를까 내가 모를는 것이 있었다!
내가 몰랐던 부분인데 다른 사람들이 풀어 놓은 풀이를 참고했다.
from collections import Counter
Counter1 = Counter(copy_split_list1)
Counter2 = Counter(copy_split_list2)
inter = list((Counter1 & Counter2).elements())
union = list((Counter1 | Counter2).elements())
우선 counter 함수는 리스트내에 있는 요소들의 수를 알려준다.
내가 정말 몰랐던 부분인 &, | 이 두개와 elements 함수?는 조금 충격적이었다..?
이 아래의 것을 참고하면 알수 있다? &교집합 |은 합집합의 수를 카운터 해주고 list 로 출력하면
다시 리스트로 알려준다,
정말 코딩에 필요한 함수는 많다
https://dongdongfather.tistory.com/70
[파이썬 기초] Counter를 이용한 항목 계산
파이썬에서 항목의 개수를 셀때 사용하는 클래스로 Counter라는게 있다. 리스트나 셋을 인자로 넘기면 각 항목을 키로 해서 개수를 알려준다. 기본사용법은 이렇다. >>> from collections import Counter >>>
dongdongfather.tistory.com
import copy
from collections import Counter
def solution(str1, str2):
str1 = str1.lower()
str2 = str2.lower()
split_list1 = []
split_list2 = []
for i in range(len(str1)-1):
split_list1.append(str1[i:i+2])
for i in range(len(str2)-1):
split_list2.append(str2[i:i+2])
copy_split_list1 = copy.deepcopy(split_list1)
copy_split_list2 = copy.deepcopy(split_list2)
for i in range(len(split_list1)):
if not split_list1[i].isalpha():
copy_split_list1.remove(split_list1[i])
for i in range(len(split_list2)):
if not split_list2[i].isalpha():
copy_split_list2.remove(split_list2[i])
if len(copy_split_list1) == 0 and len(copy_split_list2) == 0:
answer = 65536
return answer
Counter1 = Counter(copy_split_list1)
Counter2 = Counter(copy_split_list2)
inter = list((Counter1 & Counter2).elements())
union = list((Counter1 | Counter2).elements())
answer = int(65536*(len(inter)/len(union)))
return answer
아쉬운점은 앞부분에 copy를 써서 하나의 메모리 공간을 더 차지했는데 더 간단하게 푸는 방법이 있는 거 같다?
그래도 나쁘지는 않게 문제를 해결한거 같다.