| Start |
|---|
| collections.Counter Counter (opens new window) is a dict sub class that allows you to easily count objects. It has utility methods for working with the frequencies of the objects that you are counting. the above code creates an object, counts, which has the frequencies of all the elements passed to the constructor. This example has the value Constructor examples Letter Counter Word Counter Recipes Get count of individual element Set count of individual element Get total number of elements in counter (4 + 2 + 0 - 3) Get elements (only those with positive counter are kept) Remove keys with 0 or negative value Remove everything Add remove individual elements |
| collections.defaultdict collections.defaultdict (opens new window)(default_factory) returns a subclass of returns a reference to a defaultdict that will create a string object with its default_factory method. A typical usage of Calling the defaultdict with a key that does not exist does not produce an error as it would in a normal dictionary. Another example with Normal dictionary methods work with the default dictionary Using |
| collections.OrderedDict The order of keys in Python dictionaries is arbitrary: they are not governed by the order in which you add them. For example: (The arbitrary ordering implied above means that you may get different results with the above code to that shown here.) The order in which the keys appear is the order which they would be iterated over, e.g. using a The Or we can create an empty Iterating through an What happens if we assign a new value to an existing key? The key retains its original place in the |
| collections.namedtuple Define a new type The second argument is the list of attributes that the tuple will have. You can list these attributes also as either space or comma separated string: or Once defined, a named tuple can be instantiated by calling the object with the necessary parameters, e.g.: Named arguments can also be used: Now you can access the attributes of the namedtuple: The first argument to the namedtuple constructor (in our example |
| collections.deque Returns a new Deques are a generalization of stacks and queues (the name is pronounced “deck” and is short for “double-ended queue”). Deques support thread-safe, memory efficient appends and pops from either side of the Though list objects support similar operations, they are optimized for fast fixed-length operations and incur O(n) memory movement costs for pop(0) and insert(0, v) operations which change both the size and position of the underlying data representation. New in version 2.4. If Changed in version 2.6: Added maxlen parameter. Source: https://docs.python.org/2/library/collections.html (opens new window) |
| collections.ChainMap
Returns a new
Anytime one has a chain of lookup values there can be a case for The Note the impact of order on which value is found first in the subsequent lookup RemarksThere are three other types available in the collections module, namely:
They each act as a wrapper around the tied object, e.g., UserDict acts as a wrapper around a dict object. In each case, the class simulates its named type. The instance's contents are kept in a regular type object, which is accessible via the data attribute of the wrapper instance. In each of these three cases, the need for these types has been partially supplanted by the ability to subclass directly from the basic type; however, the wrapper class can be easier to work with because the underlying type is accessible as an attribute. |
Read Article
https://develop.horje.com/learn/1434/reference