Reference Python 标准库 reference

Python collections.OrderedDict 参考

Python collections.OrderedDict 参考,整理标准库调用语法、参数、示例、返回值和常见错误。

语法

python
from collections import OrderedDict

items = OrderedDict()

参数

  • iterable / mapping:用于初始化集合对象的数据。
  • default_factory:defaultdict 创建缺省值时使用的工厂函数。

返回值

返回 Counter、defaultdict、deque 等容器对象或统计结果。

示例

python
from collections import OrderedDict

items = OrderedDict([("a", 1)])
print(items)

常见错误

  • defaultdict 的默认工厂要传函数本身,不要传调用结果。
  • Counter 统计的是可哈希元素,不可哈希对象不能直接作为键。

相关内容