최대 1 분 소요


from collections import Counter

print(Counter('hello'))

# Counter({'l': 2, 'h': 1, 'e': 1, 'o': 1})

데이터 갯수를 Dictionary 형태로 반환해준다.

</br>

Methods


</br>

Counter.most_common(n)

</br>

빈도수 정렬후 상위 n개 반환.

Counter('hello world').most_common()
Counter('hello world').most_common(1)

</br>

Counter.elements()

Counter객체를 list형태로 반환

c = Counter('hello')
print(sorted(c.elements()))

# ['e', 'h', 'l', 'l', 'o']

</br>

Counter.update()

다른 Counter 객체와의 덧셈 연산

c = Counter('hello')
d = Counter('hhh')
c.update(d)

# Counter({'h': 4, 'l': 2, 'e': 1, 'o': 1})

</br>

Counter.subtract()

다른 Counter 객체와의 뺄셈 연산

c = Counter('hello')
d = Counter('hhh')
c.subtract(d)

# Counter({'l': 2, 'e': 1, 'o': 1, 'h': -2})

</br>

Counter.values

갯수를 list로 반환

c = Counter('hello')
print(c.values())

# dict_values([1, 1, 2, 1])

</br>

Counter.items

(element, count)형태의 튜플로 반환

c = Counter('hello')
print(c.items())

# dict_items([('h', 1), ('e', 1), ('l', 2), ('o', 1)])

</br>

Counter.clear

객체를 비움

c = Counter('hello')
print(c.clear())

# None

댓글남기기