[Python] Collections 모듈 ☑️

2022. 10. 22. 21:13·Python

 

collections.Counter(리스트)
리스트에서 요소들의 개수를 새고 {요소:개수} 형태의 딕셔너리로 값을 반환한다.
collections.Counter() 함수로 반환된 딕셔너리 값끼리는 연산(+, -, &, |)이 가능하다.

collections.Counter(리스트).most_common()
리스트 구성 요소들의 개수를 세고 최빈값 n을 반환한다

 

import collections

list = [1,1,1,1,2,2,2,3,3,4,5]

print(collections.Counter(list))
# Counter({1: 4, 2: 3, 3: 2, 4: 1, 5: 1})

print(collections.Counter(list).most_common())
# [(1, 4), (2, 3), (3, 2), (4, 1), (5, 1)]
'Python' 카테고리의 다른 글
  • [Python] 2차원 행렬을 90도 회전시키는 방법 ☑️
  • [Python] 배열을 선언하는 방법 ☑️
  • [Python] 분수를 표현하는 방법 (Fraction) ☑️
  • [Python] int값을 각 자리수로 이루어진 list로 바꾸는 방법 ☑️
vysryoo
vysryoo
  • vysryoo
    vysryoo
    vysryoo
  • 전체
    오늘
    어제
    • 분류 전체보기 (129)
      • Python (20)
      • Data structure (12)
      • Algorithm (14)
      • Operating system (18)
      • Programming language theory (12)
      • Computer architecture (6)
      • Softeware engineering (8)
      • Multicore (2)
      • Data Base (3)
      • Problem solving (24)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
vysryoo
[Python] Collections 모듈 ☑️
상단으로

티스토리툴바