Programming/Python
Array(list)의 원소 개수 카운팅
Chan_찬
2020. 6. 27. 10:53
반응형
collections 의 defaultdict로 배열의 엘러먼트 카운트를 하나의 함수도 대신하기
collections.Counter를 사용하면된다
from collections import defaultdict
s = ['a', 'b', 'c', 'b', 'a', 'b', 'c']
dd = defaultdict(int)
for k in s:
dd[k]+=1
print(dd)
defaultdict(<class 'int'>, {'a': 2, 'b': 3, 'c': 2})
>>> from collections import Counter
>>> d = Counter(s)
>>> print(d)
Counter({'b': 3, 'a': 2, 'c': 2})
728x90
반응형
BIG