The class Counter
basically iterates over the string and for each character it increments the value in the dictionary:
texto = 'anderson carlos woss'
quantidade = {}
for caractere in texto:
quantidade[caractere] = quantidade.get(caractere, 0) + 1
print(quantidade)
# {'a': 2, 'n': 2, 'd': 1, 'e': 1, 'r': 2, 's': 4, 'o': 3, ' ': 2, 'c': 1, 'l': 1, 'w': 1}
In fact, the Counter
class is a specialization of dict
that uses the update
method:
class Counter(dict):
def __init__(self, iterable=None, **kwds):
'''Create a new, empty Counter object. And if given, count elements
from an input iterable. Or, initialize the count from another mapping
of elements to their counts.
>>> c = Counter() # a new, empty counter
>>> c = Counter('gallahad') # a new counter from an iterable
>>> c = Counter({'a': 4, 'b': 2}) # a new counter from a mapping
>>> c = Counter(a=4, b=2) # a new counter from keyword args
'''
self.update(iterable, **kwds)
The update
method is overridden by the class as:
def update(self, iterable=None, **kwds):
if iterable is not None:
if isinstance(iterable, Mapping):
if self:
self_get = self.get
for elem, count in iterable.iteritems():
self[elem] = self_get(elem, 0) + count
else:
dict.update(self, iterable) # fast path when counter is empty
else:
self_get = self.get
for elem in iterable:
self[elem] = self_get(elem, 0) + 1
if kwds:
self.update(kwds)
That, in a very simplified way, is what I presented initially.
To return the items in order of frequency, simply check the implementation of the most_common
method:
def most_common(self, n=None):
if n is None:
return sorted(self.iteritems(), key=_itemgetter(1), reverse=True)
return _heapq.nlargest(n, self.iteritems(), key=_itemgetter(1))
That is, just use the sorted
function.
All source code for the class can be viewed at link