Counter - 개수 구하기

bok·2023년 3월 1일
0

module

목록 보기
2/2

iterable 에 대해 요소 별 개수를 구해주는 모듈

from collections import Counter


if __name__ == '__main__':
    numbers = [1, 1, 2, 2, 2, 3, 3, 3, 3]
    counter = Counter(numbers)

    # Counter({3: 4, 2: 3, 1: 2})
    print(counter)

    # [(3, 4), (2, 3), (1, 2)]
    print(counter.most_common())

    # [(3, 4)]
    print(counter.most_common(1))

most_common

  • 가장 많이 등장한 요소를 (key, count) tuple 로 반환
profile
😊

0개의 댓글