python中的容器类——collections
本文简单梳理了 python 中的容器类 collections 提供的便利工具集(wrapper toolset)。
collections — Container datatypes
namedtuple()
(命名元组): factory function for creating tuple subclasses with named fieldsdeque
(双端队列): list-like container with fast appends and pops on either endChainMap
(链接字典): dict-like class for creating a single view of multiple mappingsCounter
(计数器): dict subclass for counting hashable objectsOrderedDict
(有序字典): dict subclass that remembers the order entries were addeddefaultdict
: dict subclass that calls a factory function to supply missing valuesUserDict
: wrapper around dictionary objects for easier dict subclassingUserList
: wrapper around list objects for easier list subclassingUserString
: wrapper around string objects for easier string subclassing
help#
>>> import collections
>>> help(collections)
Help on package collections:
NAME
collections
DESCRIPTION
This module implements specialized container datatypes providing
alternatives to Python's general purpose built-in containers, dict,
list, set, and tuple.
* namedtuple factory function for creating tuple subclasses with named fields
* deque list-like container with fast appends and pops on either end
* ChainMap dict-like class for creating a single view of multiple mappings
* Counter dict subclass for counting hashable objects
* OrderedDict dict subclass that remembers the order entries were added
* defaultdict dict subclass that calls a factory function to supply missing values
* UserDict wrapper around dictionary objects for easier dict subclassing
* UserList wrapper around list objects for easier list subclassing
* UserString wrapper around string objects for easier string subclassing
OrderedDict#
Converting dict to OrderedDict
import collections
Joe = {"Age": 28, "Race": "Latino", "Job": "Nurse"}
Bob = {"Age": 25, "Race": "White", "Job": "Mechanic", "Random": "stuff"}
#Just for clarity:
Joe = collections.OrderedDict(Joe)
Bob = collections.OrderedDict(Bob)
print(Joe)
# OrderedDict([('Age', 28), ('Race', 'Latino'), ('Job', 'Nurse')])
print(Bob)
# OrderedDict([('Age', 25), ('Race', 'White'), ('Job', 'Mechanic'), ('Random', 'stuff')])
OrderedSet#
Does Python have an ordered set?