The Counter class implements what is generally called in math a multiset, a modification of the concept of a set that, unlike a set, allows for multiple instances for each of its elements. In other languages this is called a bag or multiset. The idea is simply that a Counter object is a dictionary where the values are counts of how many times the key has occurred. A set can only contain an element once but for a Counter the element can occur multiple times indicated by its count.
You can create a Counter object by using its constructor either with an iterable or a dictionary in which case the key values are used as key counts. For example:
The elements in a Counter have to be hashable and the counts can be negative or zero.
A Counter instance acts like a dictionary, but it returns a count of zero rather than an exception if the key isn’t present.
The idea that a Counter is a bag, i.e. a set with possibly repeated elements, is supported by the elements() method which returns an iterator which runs over the elements including the repeats implied by the count values. For example:
print(list(count.elements()))
where count is as in the previous example, displays:
Notice that the order of the elements is the same as they were encountered in the iterable. Using the iterator in a for loop means that the loop repeats for as many times as each element is in the bag.
There are some useful methods for working with Counter instances as frequencies:
most_common(n) returns the n most common elements as a list of tuples in the form (key, count)
total() returns a total of the counts
subtract(iterable) subtracts the counts from the existing Counter.
There are also operators that combine Counter instances in useful ways:
a+b adds a and b, i.e. a[key]+b[key]
+b is the same as +b[key]
a-b subtracts b from a, i.e. a[key]-b[key]
-b is the same as -b[key]
a & b intersection of a and b, i.e. min(a[key],b[key])
a | b union of a and b i.e. max(a[key],b[key])
All of these operations remove elements with counts of zero or less.
In chapter but not in this extract
Binary Tree
Complete Listing
Summary
The LIFO stack is a fundamental data structure and it can be implemented using a Python list.
The key feature of a LIFO stack is that it reverses the order of items added to it.
The queue, or FIFO stack, is also fundamental and it too can be implemented using a Python list, or more efficiently using a Python deque.
A queue does not change the order of items added to it.
A double-ended queue or deque is implemented using the Python deque.
A deque has characteristics of a LIFO and a FIFO stack.
Named tuples are similar to structs or records in other languages.
Named tuples are immutable.
The Counter is similar to what other languages call a bag or a multiset. It works like a dictionary or set, but stores the count of the number of times each element has been added.
Python does not have a standard binary tree data structure. However, adding one is easy and an excellent example of using stacks and queues.