본문 바로가기

Problem Solving/Python

[Python] list 최빈값 구하기

from collections import Counter

n_list = list(map(int, input().split()))	# 1 1 2 2 2 2 2 3 3 4 4 4 4 4 4 4 4 5

cnt = Counter(n_list)	# Counter({4: 8, 2: 5, 1: 2, 3: 2, 5: 1})
cnt = cnt.most_common()	# [(2, 5), (4, 4), (3, 3), (1, 2), (5, 2)]

print(cnt[0][0])	# 2

'Problem Solving > Python' 카테고리의 다른 글

[Python] 실행 시간 측정  (0) 2023.04.01