其实defaultdict 就是一个字典,只不过python自动的为它的键赋了一个初始值。这也就是说,你不显示的为字典的键赋初值python不会报错,看下实际例子。

比如你想计算频率

  1. frequencies = {}
  2. for word in wordlist:
  3. frequencies[word] += 1

python会抛出一个KeyError 异常,因为字典索引之前必须初始化,可以用下面的方法解决

  1. for word in wordlist:
  2. try:
  3. frequencies[word] += 1
  4. except KeyError:
  5. frequencies[word] = 1
  1. for word in wordlist:
  2. if word in frequencies:
  3. frequencies[word] += 1
  4. else:
  5. frequencies[word] = 1

当然,collections.defaultdict也可以轻松的解决这个问题

  1. from collections import defaultdict
  2. frequencies = defaultdict(int) #传入int()函数来初始化
  3. for word in wordlist:
  4. frequencies[word] += 1

collections.defaultdict可以接受一个函数作为参数来初始化。什么意思呢,看上面的例子,我们想要frequencies[word]初始化为0,这时就可以用一个int()函数作为参数出给defaultdict,我们不带参数调用int(),int()就会返回一个0值

Python collections.defaultdict 笔记的更多相关文章

  1. python collections defaultdict

    class_counts  = defaultdict(int) 一.关于defaultdict 在Python里面有一个模块collections,解释是数据类型容器模块.这里面有一个collect ...

  2. Python collections.defaultdict() 与 dict的使用和区别

    看样子这个文档是难以看懂了.直接看示例: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import collections s = [('yellow', ...

  3. python collections.Counter笔记

    Counter是dict的子类,所以它其实也是字典.只不过它的键对应的值都是计数,值可以是任意整数.下面是四种创建Counter实例的例子: >>> c = Counter() # ...

  4. Python 3 collections.defaultdict() 与 dict的使用和区别

    综述: 这里的defaultdict(function_factory)构建的是一个类似dictionary的对象,其中keys的值,自行确定赋值,但是values的类型,是function_fact ...

  5. Python: dict setdault函数与collections.defaultdict()的区别

    setdault用法 >>>dd={'hy':1,'hx':2} >>>cc=dd.setdefault('hz',1) >>>cc      返 ...

  6. (转)Python 3 collections.defaultdict() 与 dict的使用和区别

    原文:https://www.cnblogs.com/herbert/archive/2013/01/09/2852843.html 在Python里面有一个模块collections,解释是数据类型 ...

  7. python collections module's defaultdict

    Collections is a high-performance container datatypes. defaultdict objects class collections.default ...

  8. python核心编程--笔记

    python核心编程--笔记 的解释器options: 1.1 –d   提供调试输出 1.2 –O   生成优化的字节码(生成.pyo文件) 1.3 –S   不导入site模块以在启动时查找pyt ...

  9. python中defaultdict的用法

    初识defaultdict 之前在使用字典的时候, 用的比较随意, 只是简单的使用dict. 然而这样在使用不存在的key的时候发生KeyError这样的一个报错, 这时候就该defaultdict登 ...

随机推荐

  1. 利用KVO监视一个view的frame

    首先,keyPath一定是frame,而不是frame.origin.x之类的路径,因为再点下去的话,就是访问结构体内部的值了,KVO是无法检测的,会报错找不到KeyPath. 代码如下: [_fun ...

  2. 使用border-image实现类似iOS7的1px底边

    使用border-image实现类似iOS7的1px底边 iOS7已经发布有一段时间,扁平化设计风格有很多值得称赞的地方,其中有很多设计细节都是值得研究的. 首先,来看下面iOS设置的截图中的bord ...

  3. Tomcat配置一个ip绑定多个域名

    在网上找了半天也没找到相关的资料,都说的太含糊. 本人对tomcat下配置 一ip对多域名的方法具体例如以下,按以下配置一定能成功,经过測试了. <Host name="localho ...

  4. linux 使用sudo开放普通用户权限

    整理一下以前写的东东,刚才又忘了- ---------------------------------------------------------------------------------- ...

  5. oracle tns

    oracle tns 是oracle提供的服务名,设置方法,oracle安装根目录---product----版本选择11.2.0----client1---NETWORK---ADMIN---tns ...

  6. Memcached基础

    1.实例化 MemcachedClient client = new XMemcachedClient(); public XMemcachedClient() public XMemcachedCl ...

  7. JS兼容性问题列表

    记录平时遇见的兼容性问题,有更好的解决办法希望各位提出,会随着开发遇到问题而更新,标记为黄色的为未解决和猜测答案 提出时间 问题描述 解决方案 2014/10/22 submit按钮阻止了默认事件不能 ...

  8. 插入数据,返回最新id

    最简单的方法就是在查询之后select @@indentity. sql代码: INSERT INTO table_name (.....) VALUES(......)  SELECT @@IDEN ...

  9. AutoCompleteTextView 自动提示

    在输入框中输入我们想要输入的信息就会出现其他与其相关的提示信息,这种效果在Android中是用AutoCompleteTextView实现的. public class MainActivity ex ...

  10. Mysql笔记之 -- replace()实现mysql 替换字符串

    mysql 替换函数replace()实现mysql 替换字符串 mysql 替换字符串的实现方法:  mysql中replace函数直接替换mysql数据库中某字段中的特定字符串,不再需要自己写函数 ...