-
[프로그래머스] 1차 뉴스 클러스터링 (Counter)코테 준비/문자열, 내장함수 2023. 1. 21. 22:39
Counter(list1).elements(): 입력된 값의 요소를 풀어서 반환
from collections import Counter ex_counter = Counter("I want success") print(list(ex_counter.elements())) >>> ['I', ' ', ' ', 'w', 'a', 'n', 't', 's', 's', 's', 'u', 'c', 'c', 'e']
from collections import Counter def multi(s): #리스트 만들어주는 함수 s=s.lower() #소문자로 통일 multi=[] for i in range(len(s)-1): if s[i:i+2].isalpha()==True: multi.append(s[i:i+2]) #두글자씩 잘라서 넣기 return multi def solution(str1, str2): str1=multi(str1) str2=multi(str2) if not str1 and not str2: # 둘다 공집합일때 return 65536 else count1=Counter(str1) count2=Counter(str2) answer=len(list((count1&count2).elements()))/len(list((count1|count2).elements())) return int(answer*65536)
'코테 준비 > 문자열, 내장함수' 카테고리의 다른 글
[백준] 5525. IOIOI (0) 2023.02.26 [프로그래머스] 연속 부분 수열 합의 개수 (0) 2023.01.24 [프로그래머스] n^2 배열 자르기 (0) 2023.01.21 [프로그래머스] 튜플 (0) 2023.01.20 [백준] 18870. 좌표압축 (정렬) (0) 2023.01.12