brennerm / PyTricks

Collection of less popular features and tricks for the Python programming language
MIT License
3.07k stars 503 forks source link

characters_occurrence.py #71

Open ghost opened 8 years ago

ghost commented 8 years ago

! /usr/bin/env python3

"""To find the number of times characters are repeated in a string """

from collections import Counter

Counter('abracadabra').most_common(3)

[('a', 5), ('r', 2), ('b', 2)]

The argument of most_common is the number of items to be returned

Default will return count for all characters

Counter('abracadabra').most_common(1)

[('a', 5)]