from collections import Counter
import sys
list = [1, 3, 2, 2, 3, 5, 2, 3, 9]
Counter() : 문자열이나, list 의 요소를 카운팅하여 많은 순으로 딕셔너리형태로 리턴한다
countValue =Counter(list)
print(countValue)
#출력 결과: Counter({3: 3, 2: 2, 1: 1, 4: 1, 5: 1, 9: 1})
most_common() : 개수가 많은 순으로 정렬된 튜플 배열 리스트를 리턴한다
countValue1=Counter(list).most_common()
print(countValue1)
#출력결과: [(3, 3), (2, 2), (1, 1), (4, 1), (5, 1), (9, 1)]
리스트의 요소가 하나인 경우
list1 = [1]
countValue3 = Counter(list1).most_common()
print(countValue3)
#출력결과: [(1, 1)]
'파이썬 알고리즘 > 알고리즘 직빵 파이썬 문법' 카테고리의 다른 글
dict 정렬 - 키 중복x, value 중복o (0) | 2022.11.15 |
---|---|
리스트의 문자열(기호포함) 각각 쪼개기 (0) | 2022.11.14 |
튜플(tuple)을 요소로 가지고 있는 리스트 접근방법 (0) | 2022.11.13 |
in vs in range (0) | 2022.11.10 |
재귀함수 처리 후 후속작업 (0) | 2022.11.09 |