-
Notifications
You must be signed in to change notification settings - Fork 0
Container data type Counter
ref: Reference for Counter datatypes
It keeps track of how many times equivalent values are added.
Initialization
It supports 3 types of initialization --
import collections
-
print collections.Counter(['a', 'b', 'c', 'a', 'b', 'b']) -
print collections.Counter({'a':2, 'b':3, 'c':1}) -
print collections.Counter(a=2, b=3, c=1)
An empty Counter can be constructed with no arguments and populated via the update() method.
import collections
c = collections.Counter()
print 'Initial :', c
c.update('abcdaab')
print 'Sequence:', c
c.update({'a':1, 'd':5})
print 'Dict :', c
$ python collections_counter_update.py
Initial : Counter()
Sequence: Counter({'a': 3, 'b': 2, 'c': 1, 'd': 1})
Dict : Counter({'d': 6, 'a': 4, 'b': 2, 'c': 1})
Accessing counts
- Once a Counter is populated, its values can be retrieved using the dictionary API.
import collections
c = collections.Counter('abcdaab')
for letter in 'abcde':
print '%s : %d' % (letter, c[letter])
- elements()
The elements() method returns an iterator that produces all of the items known to the Counter.
Example: import collections
c = collections.Counter('extremely')
c['z'] = 0
print(c)
print(list(c.elements()))
output --
$ python collections_counter_elements.py
Counter({'e': 3, 'm': 1, 'l': 1, 'r': 1, 't': 1, 'y': 1, 'x': 1, 'z': 0})
['e', 'e', 'e', 'm', 'l', 'r', 't', 'y', 'x']
- Use most_common() to produce a sequence of the n most frequently encountered input values and their respective counts.
Example:
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 '%s: %7d' % (letter, count)`
This example counts the letters appearing in all of the words in the system dictionary to produce a frequency distribution, then prints the three most common letters. Leaving out the argument to most_common() produces a list of all the items, in order of frequency.
$ python collections_counter_most_common.py
Most common:
e: 235331
i: 201032
a: 199554
Must read: All the necessary tools of python
contact: sumo.77sg@gmail.com
Data structure algos