List - 요소 별 Count
Coding Practice/Python2024. 7. 16. 19:02List - 요소 별 Count

List는 가장 기본이 되는 자료구조이다.종종, List 내 요소들에 대해 Counting을 알고 싶어질 때가 있는데,그럴 땐 Collections 라이브러리의 Counter 함수를 활용하면 된다.List의 요소가 문자열이 아닌 숫자여도 동일하게 사용하면 된다.from collections import Counter# 예시 리스트elements = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple', 'kiwi', 'kiwi']# Counter를 사용하여 요소별 개수 세기element_counts = Counter(elements)# 결과 출력print(element_counts)#Counter({'apple': 3, 'banana': 2, 'orange..

image