Notice
Recent Posts
Recent Comments
Link
목록Collections (11)
SeouliteLab
Python에서 Collection 개수 세는 방법 - Counter 활용하기
1. 리스트의 항목 개수 세기 Counter를 사용하여 리스트의 각 항목의 개수를 세어볼 수 있습니다. Counter는 collections 모듈에 포함되어 있으며, 각 항목의 개수를 사전 형태로 반환합니다. from collections import Counter items = ['apple', 'banana', 'apple', 'orange', 'apple', 'banana'] item_counts = Counter(items) print(item_counts) 2. 문자열의 문자 개수 세기 Counter를 사용하여 문자열의 각 문자의 개수를 세어볼 수도 있습니다. 문자열은 iterable한 객체이므로 Counter에 넘겨주면 각 문자의 개수를 세어줍니다. string = "hello" char_co..
프로그래밍
2024. 3. 2. 12:30