collection系列用法-defaultdict()
defaultdict()
定义以及作用
返回一个和dictionary类似的对象,和dict不同主要体现在2个方面:
- 可以指定key对应的value的类型。
- 不必为默认值担心,换句话说就是不必担心有key没有value这回事。总会有默认的value.
from collections import defaultdict
s=[('yellow',1),('blue',2),('yellow',3),('blue',4),('red',5)]
d=defaultdict(list)
for k,v in s:
d[k].append(v)
print list(d.items())
d_2 = {}
for k, v in s:
d_2.setdefault(k, []).append(v)
print list(d_2.items()) d_3 = {}
for k, v in s:
d_3[k].append(v)
print d_3.items()
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsMAAAC8CAIAAAASBTQoAAAgAElEQVR4nO2du2scy5fH798ijGe4GIOce4dNdo1Hgt9cuMnsTdfiDlxdsUjKFodWYARWNLmFwTDJRdEkVmYUGkUalDlQanCkzTbo6p56nVOPrurHzLf5JFL3VNeju+rbp06d+uUBBw7b8a9//Wsw3AEAAAB4fml7wMLR0WMzlMR//Oe/t54HAADYbKAkcNiPzVASAAAAcgMlgcN+bIaSgE0CAAByAyWBw35shpIAAACQGygJHPZjM5QEbBJ9ZDK/fVwetZ6NXmDW1WR+ezcfM9czZwGIA0oCh/3YDCXRc/5t+fR//u/pf88HTd/65ccfb1b/vCQv+Pu31Y83qx+//Zn+1pP57ePjz+Upc83R8vFng8Ph0fLx5+Pj7XzSdCtE1dV4vvr5SNYPfxaASH55gQMHDhw4cODAEXv88r84cODAgQMHDhyxxy//hQMHDhw4cODAEXtASeDAgQMHDhw44g8oCRw4cODAgQNH/PFL6z6fAAAAAOgvUBIAAAAAiAdKAgAAAADx1FUSL959e/Px79aLAYJoJs4dIhXWYe/s4OZst4G7PFyOWi8saAzEuAQ5oJSEiGEnILTCi3ffiDh3xc+/vXqt/N8zcN6bXOHz7LlKlKyjrhrm5UcpS3qdNxHnjo5UOFrcHz/cH5yP8xV/tLg/NobhTPctki0JGZUPL6Uf3k8PlbO759fHD5ZSpGTv7ODh/ngxa6aNlIoybsqfTXX35K2/ae/+yfL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d3bwcDky8gYAAAD44rUKVJYOylyGHaE2LFdmUhKm8cM25+JSEun9JAbDnUoxmDMXvssl4vwkyHmT3fPr45uz0fl1pW+mh7MpFnwCAACIwzcyFeMXaVItttQH71xKwr52Y32xj5JIv3ZDUCydsKiB8f6Nj1ki0dqN6prDy+OHe3XORV9jAgAAAPgSEC1bmj7QvSX00FVijYbhsMlHy66jJIaVFLD5irZpkygVg/2jX/eWWM9KzKY2L4pyDoI/q1+g3FpeFFr9iSWgAAAA4sizg5eH3WKL8LQ9AAAAAD0ki5KwRWvYXsiQUwAAAED/yaAk0m7V0XdUlwUAAABgw0ipJKqQVpu4R1cwwrERcRoAAABsNHn8JAAAAACwHfw/3yK24jUOPYgAAAAASUVORK5CYII=" alt="" />
d = defaultdict(list),该语句创建一个defaultdict类型(你可以想象为dict类型),value的类型是你自己指定的类型,实例是list。
d_3是普通的dict,key值必须是唯一,当key重复的时候出现异常。defaultdict是可以直接就进行d[k]的操作,即使d此时还是一个空的字典。
实际过程就是示例里d_2的处理过程。
小提示:
Python 字典(Dictionary) setdefault() 函数和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值。
语法
setdefault()方法语法:
dict.setdefault(key, default=None)
参数
- key -- 查找的键值。
- default -- 键不存在时,设置的默认键值。
返回值
该方法没有任何返回值。
实例
以下实例展示了 setdefault()函数的使用方法:
#!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7} print "Value : %s" % dict.setdefault('Age', None)
print "Value : %s" % dict.setdefault('Sex', None)
以上实例输出结果为:
Value : 7
Value : None
collection系列用法-defaultdict()的更多相关文章
- collection系列用法-deque双向队列
deque双向队列 Deque可以从两端添加和删除元素.常用的结构,是它的简化版本. Deque支持序列的常用操作,现在举一个简单例子,你会发现其实跟平成的list没啥区别: import colle ...
- collection系列用法-namedtuple()
namedtuple() 参考文章地址:http://www.cnblogs.com/herbert/p/3468294.html namedtuple是继承自tuple的子类.namedtuple和 ...
- 3.python基础补充(集合,collection系列,深浅拷贝)
一.集合 1.集合(set): 把不同的元素组成一起形成集合,是python基本的数据类型.集合元素(set elements):组成集合的成员 python的set和其他语言类似, 是一个无序不重复 ...
- Python 从零学起(纯基础) 笔记 之 collection系列
Collection系列 1. 计数器(Counter) Counter是对字典类型的补充,用于追踪值的出现次数 ps 具备字典所有功能 + 自己的功能 Counter import col ...
- python开发学习-day03(set集合、collection系列 、深浅拷贝、函数)
s12-20160116-day03 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- python collection系列
collection系列 不常用功能,需要进行模块功能导入: import collection Counter 常用方法测试: #!/usr/local/env python3 ''' Author ...
- Java系列:Collection.toArray用法研究
该方法的签名如下: <T> T[] Collection.toArray(T[] arrayToFill); 这里想验证两个问题: 1)arrayToFill什么时候会被填充: 2)arr ...
- 八、collection系列-----计数器、有序字典、默认字典、可命名元组、双向队列、单向队列一.计数器(对字典的扩展)
一.计数器(对字典的扩展) 有如下一个字典: dic = {'k1':123,'k2':123,'k3':12} 统计12出现的次数,123出现的次数 1.统计出现次数 >>> ...
- 基础(三):yum(RedHat系列)和apt-get(Debian系列 )用法及区别
文章转载来自:http://blog.csdn.net/chengly0129/article/details/70169760 一般来说著名的linux系统基本上分两大类:1.RedHat系列:Re ...
随机推荐
- hdu 2509 Be the Winner 博弈
题目链接 有n堆苹果, 对于其中的每一堆的x个苹果, 它是放在一条线上的. 你每次可以对一堆苹果进行操作, 可以取y个, 1<=y<=x. 然后如果你是取的一条线上中间的苹果, 那么这一堆 ...
- C#dll版本号默认生成规则
原文:C#dll版本号默认生成规则 1.版本号自动生成方法 只需把 AssemblyInfo.cs文件中的[assembly: AssemblyVersion("1.0.0.0") ...
- 读懂系统负载(Load Avg)的含义 | Devops
有过运维Linux服务器的选手,想必对于系统平均负载(load averages)参数不会陌生吧,我们可以通过top, htop, uptime这些命令找到它们(如下图),那么我们又改如何理解它们呢, ...
- libcurl提交表单上传文件
不多说了,curl的http上传文件代码示例,有需要的可以参考. int http_post_file(const char *url, const char *user, const char *p ...
- 黑马程序员_static\访问权限\单例模式 大汇总
一.static关键字 1.用法 Static是一个修饰符,用于修饰成员,包括成员变量和成员函数.当成员被静态修饰后,就多了一种调用方式,除了可以被对象调用外,还可以直接被类名调用.System.ou ...
- ubuntu下perl SVG老是make失败
解决方法是用libgd-svg-perl软件包代替.
- Linux学习之十三、快捷键与通配符、数据流重导向
通配符与特殊符号 接下来让我们利用通配符来玩些东西吧!首先,利用通配符配合 ls 找檔名看看: [root@www ~]# LANG=C <==由于与编码有关,先配置语 ...
- Triangle LOVE(拓扑排序)
Triangle LOVE Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other) Total ...
- Linux下实现视频读取(二)---camera參数设定
Camera的可设置项极多,V4L2 支持了不少.但Sam之前对这些设置的使用方法和涵义都是在看videodev2.h中边看边理解.感觉很生涩. 直到写这篇blog时,才发现v4l2有专门的SPEC来 ...
- OCP prepare 20140703
1. trim trim('aaa' from 'aaabbbccc') 这个是错误的.ora-30001: trim set should have only one character 2. in ...