Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- KAKAO
- python 갯수세기
- Scienceplots
- 스택
- 코테
- Overleaf
- 에러해결
- Python
- Pycaret
- MAPE
- 평가지표
- TypeError
- Mae
- 논문
- 파이썬을파이썬답게
- PAPER
- 프로그래머스
- RMES
- Tire
- mes
- n_neighbors
- SMAPE
- Alignments
- 카카오
- 논문editor
- knn
- 논문작성
- mMAPE
- n_sample
- iNT
Archives
- Today
- Total
EunGyeongKim
[Python] collection모듈의 counter 본문
collections — Container datatypes — Python 3.10.6 documentation
collections — Container datatypes Source code: Lib/collections/__init__.py This module implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple. namedtuple() factory f
docs.python.org
collectoins모듈의 Counter 예제코드
counter : A Counter is a dict subclass for counting hashable objects
import collections
#counter 함수를 써서 list안의 갯수 파악 가능!
name_list = ["muzi", "frodo","apeach", "frodo","frodo","neo","muzi","neo","apeach","muzi"]
c = collections.Counter(name_list)
# Counter({'apeach': 2, 'frodo': 3, 'muzi': 3, 'neo': 2})
elements()
# elements를 써서 구성요소 return
list(c.elements())
#['muzi', 'muzi', 'muzi', 'frodo', 'frodo', 'frodo', 'apeach', 'apeach', 'neo', 'neo']
most_common[n]
: n개의 가장 일반적인 요소의 목록과 가장 일반적인 것부터 가장 적은 것까지의 개수(n)를 반환
c.most_common(2)
#[('muzi', 3), ('frodo', 3)]
응용
import collections
name_list = ["muzi", "frodo","apeach", "frodo","frodo","neo","muzi","neo","apeach","muzi"]
c = collections.Counter(name_list)
# count한 key와 value 출력
for i in c:
print(i, c[i])
# output
# muzi 3
# frodo 3
# apeach 2
# neo 2
'Language > Python' 카테고리의 다른 글
[Python] 유클리드 호제법 (0) | 2022.09.06 |
---|---|
[데이터 구조 및 분석] Linked List 코드 (0) | 2022.08.12 |
[paper] Matplotlib으로 논문용 figure그리기 (1) | 2022.08.10 |
[Python] int 진법 변환 시 에러 (0) | 2022.08.05 |
[프로그래머스] 파이썬을 파이썬답게 정리 (0) | 2022.08.04 |
Comments