defaultdict python3,Python collections.defaultdict() 与 dict的使用和区别|python3教程|python入门|python教程...

https://www.xin3721.com/eschool/python.html

在Python里面有一个模块collections,解释是数据类型容器模块。这里面有一个collections.defaultdict()经常被用到。主要说说这个东西。

综述:

这里的defaultdict(function_factory)构建的是一个类似dictionary的对象,其中keys的值,自行确定赋值,但是values的类型,是function_factory的类实例,而且具有默认值。比如default(int)则创建一个类似dictionary对象,里面任何的values都是int的实例,而且就算是一个不存在的key, d[key] 也有一个默认值,这个默认值是int()的默认值0.

defaultdict

dict subclass that calls a factory function to supply missing values。

这是一个简短的解释

defaultdict属于内建函数dict的一个子类,调用工厂函数提供缺失的值。

比较晕,什么是工厂函数:

来自python 核心编程的解释

Python 2.2 统一了类型和类, 所有的内建类型现在也都是类, 在这基础之上, 原来的

所谓内建转换函数象int(), type(), list() 等等, 现在都成了工厂函数。 也就是说虽然他

们看上去有点象函数, 实质上他们是类。当你调用它们时, 实际上是生成了该类型的一个实

例, 就象工厂生产货物一样。

下面这些大家熟悉的工厂函数在老的Python 版里被称为内建函数:

int(), long(), float(), complex()

str(), unicode(), basestring()

list(), tuple()

type()

以前没有工厂函数的其他类型,现在也都有了工厂函数。除此之外,那些支持新风格的类

的全新的数据类型,也添加了相应的工厂函数。下面列出了这些工厂函数:

dict()

bool()

set(), frozenset()

object()

classmethod()

staticmethod()

super()

property()

file()

再看看它的使用:

这里就开始有点明白了,原来defaultdict可以接受一个内建函数list作为参数。其实呢,list()本身是内建函数,但是再经过更新后,python里面所有东西都是对象,所以list改编成了类,引入list的时候产生一个类的实例。

还是不太明白,再看defaultdict的help解释

class collections.defaultdict([default_factory[, ...]])

Returns a new dictionary-like object. defaultdict is a subclass of the built-in dict class. It overrides one method and adds one writable instance variable. The remaining functionality is the same as for the dict class and is not documented here.

首先说了,collections.defaultdict会返回一个类似dictionary的对象,注意是类似的对象,不是完全一样的对象。这个defaultdict和dict类,几乎是一样的,除了它重载了一个方法和增加了一个可写的实例变量。(可写的实例变量,我还是没明白)

The first argument provides the initial value for the default_factory attribute; it defaults to None. All remaining arguments are treated the same as if they were passed to the dict constructor, including keyword arguments.

defaultdict objects support the following method in addition to the standard dict operations:

__missing__(key)

If the default_factory attribute is None, this raises a KeyError exception with the key as argument.

If default_factory is not None, it is called without arguments to provide a default value for the given key, this value is inserted in the dictionary for the key, and returned.

主要关注这个话,如果default_factory不是None, 这个default_factory将以一个无参数的形式被调用,提供一个默认值给___missing__方法的key。 这个默认值将作为key插入到数据字典里,然后返回。

十分晕。有扯出了个__missing__方法,这个__missing__方法是collections.defaultdict()的内建方法。

If calling default_factory raises an exception this exception is propagated unchanged.

This method is called by the __getitem__() method of the dict class when the requested key is not found; whatever it returns or raises is then returned or raised by __getitem__().

Note that __missing__() is not called for any operations besides __getitem__(). This means that get() will, like normal dictionaries, return None as a default rather than using default_factory.

defaultdict objects support the following instance variable:

default_factory

This attribute is used by the __missing__() method; it is initialized from the first argument to the constructor, if present, or to None, if absent.

看样子这个文档是难以看懂了。直接看示例:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值