Python built-in module collections tutorial, pythoncollections

Source: Internet
Author: User

Python built-in module collections tutorial, pythoncollections

Collections is a collection module built in Python and provides many useful collection classes.
Namedtuple

We know that tuple can represent the unchanged set. For example, the two-dimensional coordinates of a point can be expressed:

>>> p = (1, 2)

However, when we see (1, 2), it is hard to see that this tuple is used to represent a coordinate.

After defining a class, I made a fuss. At this moment, namedtuple came in handy:

>>> from collections import namedtuple>>> Point = namedtuple('Point', ['x', 'y'])>>> p = Point(1, 2)>>> p.x1>>> p.y2

Namedtuple is a function used to create a custom tuple object and specify the number of tuple elements. You can use attributes instead of indexes to reference an element of tuple.

In this way, we can use namedtuple to easily define a data type. It has the immutability of tuple and can be referenced Based on attributes, which is very convenient to use.

You can verify that the created Point object is a subclass of tuple:

>>> isinstance(p, Point)True>>> isinstance(p, tuple)True

Similarly, if you want to use coordinates and radius to represent a circle, you can also use namedtuple to define it:

# Namedtuple ('name', [attribute list]): Circle = namedtuple ('circle', ['x', 'y', 'R'])

Deque

When using list to store data, index-based access elements are fast, but inserting and deleting elements is slow, because list is linear storage, and insertion and deletion efficiency is low when there is a large amount of data.

Deque is a two-way list for efficient insert and delete operations. It is suitable for queue and stack:

>>> from collections import deque>>> q = deque(['a', 'b', 'c'])>>> q.append('x')>>> q.appendleft('y')>>> qdeque(['y', 'a', 'b', 'c', 'x'])

In addition to list append () and pop (), deque also supports appendleft () and popleft (). This allows you to efficiently add or delete elements to or from the header.
Defaultdict

When using dict, if the referenced Key does not exist, a KeyError is thrown. If you want to return a default value when the key does not exist, you can use defaultdict:

>>> From collections import defaultdict >>> dd = defaultdict (lambda: 'n'/A') >>> dd ['key1'] = 'abc' >>> dd ['key1'] # 'abc' exists in key1 >>> dd ['key2'] # key2 does not exist, returns the default value 'n'/'

Note that the default value is returned by calling the function, and the function is passed in when the defaultdict object is created.

Besides returning the default value when the Key does not exist, defaultdict performs the same behavior as dict.
OrderedDict

When using dict, keys are unordered. During the iteration of dict, we cannot determine the Key order.

To maintain the order of keys, use OrderedDict:

>>> From collections import OrderedDict >>> d = dict ([('A', 1), ('B', 2), ('C ', 3)]) >>> d # the keys of dict are unordered {'A': 1, 'C': 3, 'B ': 2 }>>> od = OrderedDict ([('A', 1), ('B', 2), ('C', 3)]) >>> od # The Key of OrderedDict is an ordered OrderedDict ([('A', 1), ('B', 2), ('C', 3)])

Note that the keys of OrderedDict are arranged in the insert order, not the keys themselves:

>>> Od = OrderedDict () >>> od ['Z'] = 1 >>> od ['y'] = 2 >>> od ['X'] = 3 >>> od. keys () # Return ['Z', 'y', 'x'] in the order of the inserted Key

OrderedDict can implement a FIFO (first-in-first-out) dict. When the capacity exceeds the limit, delete the first Key to be added:

from collections import OrderedDictclass LastUpdatedOrderedDict(OrderedDict):  def __init__(self, capacity):    super(LastUpdatedOrderedDict, self).__init__()    self._capacity = capacity  def __setitem__(self, key, value):    containsKey = 1 if key in self else 0    if len(self) - containsKey >= self._capacity:      last = self.popitem(last=False)      print 'remove:', last    if containsKey:      del self[key]      print 'set:', (key, value)    else:      print 'add:', (key, value)    OrderedDict.__setitem__(self, key, value)

Counter

Counter is a simple Counter. For example, count the number of characters:

>>> from collections import Counter>>> c = Counter()>>> for ch in 'programming':...   c[ch] = c[ch] + 1...>>> cCounter({'g': 2, 'm': 2, 'r': 2, 'a': 1, 'i': 1, 'o': 1, 'n': 1, 'p': 1})

Counter is actually a subclass of dict. The preceding results show that the 'g', 'M', And 'r' characters appear twice, and the other characters appear once.
Summary

The collections module provides some useful collection classes that can be selected as needed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.