Initializing

Counter supports three forms of initialization.


In [16]:
import collections

In [10]:
# called with a sequence of item
init_list = collections.Counter(['a', 'b', 'c', 'a', 'b', 'b'])
init_str = collections.Counter({'a': 2, 'b': 3, 'c': 1})

In [11]:
print(init_list)
print('*'*40)
print(init_str)


Counter({'b': 3, 'a': 2, 'c': 1})
****************************************
Counter({'b': 3, 'a': 2, 'c': 1})

In [12]:
# a dictionary containing keys and counts
init_dict = collections.Counter({'a': 2, 'b': 3, 'c': 1})
print(init_dict)


Counter({'b': 3, 'a': 2, 'c': 1})

In [13]:
# keyword arguments that map string to counts
init_keyword=collections.Counter(a=2,b=3,c=1)
print(init_keyword)


Counter({'b': 3, 'a': 2, 'c': 1})

In [20]:
# keyword arguments that map string to counts
# the count can be any integer value, including, \
# zero and negative counts
init_nonpositive=collections.Counter(a=2,b=3,c=1,d=-1,e=0)
print(init_nonpositive)


Counter({'b': 3, 'a': 2, 'c': 1, 'e': 0, 'd': -1})

In [14]:
# of course, we can build empty Counter
init_empty = collections.Counter()
print(init_empty)


Counter()

Accessing

Using dictionary API


In [17]:
import collections
c = collections.Counter('abcdaab')

for letter in 'abcde':
    print('{} : {}'.format(letter, c[letter]))


a : 3
b : 2
c : 1
d : 1
e : 0

Note:
Counter don't raise KeyError for unknown items, If a value has not seen in the input, its count is 0

elements()

The elements() method returns an iterator that produces all of the items known to the Counter.

  • each element repeating each as many as times as its count.
  • elements returned in arbitrary order.
  • if an element's count is less then one, elements method will ignore it.

In [21]:
import collections

c = collections.Counter('extremely')
c['z'] = 0
print(c)
print(list(c.elements()))


Counter({'e': 3, 'x': 1, 't': 1, 'r': 1, 'm': 1, 'l': 1, 'y': 1, 'z': 0})
['e', 'e', 'e', 'x', 't', 'r', 'm', 'l', 'y']

most_common()

return a list of the n most elements and their counts from the most common to the least. If n is ommited or None, returns all elments in the counter.


In [22]:
import collections

c = collections.Counter()
with open('/usr/share/dict/words', 'rt') as f:
    for line in f:
        c.update(line.rstrip().lower())

print('Most common:')
for letter, count in c.most_common(3):
    print('{}: {:>7}'.format(letter, count))


Most common:
s:   90113
e:   88833
i:   66986

Updating

update()


In [25]:
# update with iterabal
import collections

c = collections.Counter('extremely')
print("before update: ", c)

print("*"*40)

c.update("aell")
print("after update:", c)


before update:  Counter({'e': 3, 'x': 1, 't': 1, 'r': 1, 'm': 1, 'l': 1, 'y': 1})
****************************************
after update: Counter({'e': 4, 'l': 3, 'x': 1, 't': 1, 'r': 1, 'm': 1, 'y': 1, 'a': 1})

In [26]:
# update with mapping
c = collections.Counter('extremely')
print("before update: ", c)

print("*"*40)

c.update({'e':10,'t':1})
print("after update:", c)


before update:  Counter({'e': 3, 'x': 1, 't': 1, 'r': 1, 'm': 1, 'l': 1, 'y': 1})
****************************************
after update: Counter({'e': 13, 't': 2, 'x': 1, 'r': 1, 'm': 1, 'l': 1, 'y': 1})

subtract()

inverse operation of update


In [27]:
import collections

c = collections.Counter('extremely')
print("before update: ", c)

print("*"*40)

c.subtract("aell")
print("after update:", c)


before update:  Counter({'e': 3, 'x': 1, 't': 1, 'r': 1, 'm': 1, 'l': 1, 'y': 1})
****************************************
after update: Counter({'e': 2, 'x': 1, 't': 1, 'r': 1, 'm': 1, 'y': 1, 'l': -1, 'a': -1})

Arithmetic

Counter instance support arithmetic and set operation for aggregating results.


In [28]:
import collections

c1 = collections.Counter(['a', 'b', 'c', 'a', 'b', 'b'])
c2 = collections.Counter('alphabet')

print('C1:', c1)
print('C2:', c2)

print('\nCombined counts:')
print(c1 + c2)

print('\nSubtraction:')
print(c1 - c2)

print('\nIntersection (taking positive minimums):')
print(c1 & c2)

print('\nUnion (taking maximums):')
print(c1 | c2)


C1: Counter({'b': 3, 'a': 2, 'c': 1})
C2: Counter({'a': 2, 'l': 1, 'p': 1, 'h': 1, 'b': 1, 'e': 1, 't': 1})

Combined counts:
Counter({'a': 4, 'b': 4, 'c': 1, 'l': 1, 'p': 1, 'h': 1, 'e': 1, 't': 1})

Subtraction:
Counter({'b': 2, 'c': 1})

Intersection (taking positive minimums):
Counter({'a': 2, 'b': 1})

Union (taking maximums):
Counter({'b': 3, 'a': 2, 'c': 1, 'l': 1, 'p': 1, 'h': 1, 'e': 1, 't': 1})

In [ ]: