python学习笔记整理——dictView [未整理]
Dictionary view objects简介
The objects returned by dict.viewkeys(), dict.viewvalues() and dict.viewitems() are view ob-
jects. They provide a dynamic view on the dictionary’s entries, which means that when the dictionary changes, the
view reflects these changes.
Dictionary views can be iterated over to yield their respective data, and support membership tests:
len(dictview)
Return the number of entries in the dictionary.
iter(dictview)
Return an iterator over the keys, values or items (represented as tuples of (key, value)) in the dictionary.
Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementa-
tions, and depends on the dictionary’s history of insertions and deletions. If keys, values and items views are
iterated over with no intervening modifications to the dictionary, the order of items will directly correspond. This
allows the creation of (value, key) pairs using zip(): pairs = zip(d.values(), d.keys()).
Another way to create the same list is pairs = [(v, k) for (k, v) in d.items()].
Iterating views while adding or deleting entries in the dictionary may raise a RuntimeError or fail to iterate
over all entries.
x in dictview
Return True if x is in the underlying dictionary’s keys, values or items (in the latter case, x should be a (key,
value) tuple).
Keys views are set-like since their entries are unique and hashable. If all values are hashable, so that (key, value) pairs
are unique and hashable, then the items view is also set-like. (Values views are not treated as set-like since the entries
are generally not unique.) Then these set operations are available (“other” refers either to another view or a set):
dictview & other
Return the intersection of the dictview and the other object as a new set.
dictview | other
Return the union of the dictview and the other object as a new set.
dictview - other
Return the difference between the dictview and the other object (all elements in dictview that aren’t in other) as
a new set.
dictview ^ other
Return the symmetric difference (all elements either in dictview or other, but not in both) of the dictview and
the other object as a new set.
An example of dictionary view usage:
>>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}
>>> keys = dishes.viewkeys()
>>> values = dishes.viewvalues()
>>> # iteration
>>> n = 0
>>> for val in values:
... n += val
>>> print(n)
504
>>> # keys and values are iterated over in the same order
>>> list(keys)
['eggs', 'bacon', 'sausage', 'spam']
>>> list(values)
[2, 1, 1, 500]
>>> # view objects are dynamic and reflect dict changes
>>> del dishes['eggs']
>>> del dishes['sausage']
>>> list(keys)
['spam', 'bacon']
>>> # set operations
>>> keys & {'eggs', 'bacon', 'salad'}
{'bacon'}
python学习笔记整理——dictView [未整理]的更多相关文章
- 大学四年的Python学习笔记分享之一,内容整理的比较多与仔细
翻到以前在大学坚持记录的Python学习笔记,花了一天的时间整理出来,整理时不经回忆起大学的时光,一眨眼几年就过去了,现在还在上学的你们,一定要珍惜现在,有个充实的校园生活.希望这次的分享对于你们有学 ...
- python学习笔记整理——字典
python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...
- canvas学习笔记、小函数整理
http://bbs.csdn.net/topics/391493648 canvas实例分享 2016-3-16 http://bbs.csdn.net/topics/390582151 html5 ...
- golang学习笔记13 Golang 类型转换整理 go语言string、int、int64、float64、complex 互相转换
golang学习笔记13 Golang 类型转换整理 go语言string.int.int64.float64.complex 互相转换 #string到intint,err:=strconv.Ato ...
- OpenCV之Python学习笔记
OpenCV之Python学习笔记 直都在用Python+OpenCV做一些算法的原型.本来想留下发布一些文章的,可是整理一下就有点无奈了,都是写零散不成系统的小片段.现在看 到一本国外的新书< ...
- python学习笔记(二)、字符串操作
该一系列python学习笔记都是根据<Python基础教程(第3版)>内容所记录整理的 1.字符串基本操作 所有标准序列操作(索引.切片.乘法.成员资格检查.长度.最小值和最大值)都适用于 ...
- python学习笔记(一)、列表和元祖
该一系列python学习笔记都是根据<Python基础教程(第3版)>内容所记录整理的 1.通用的序列操作 有几种操作适用于所有序列,包括索引.切片.相加.相乘和成员资格检查.另外,Pyt ...
- Python学习笔记(四)函数式编程
高阶函数(Higher-order function) Input: 1 abs Output: 1 <function abs> Input: 1 abs(-10) Output: 1 ...
- Python学习笔记(十三)
Python学习笔记(十三): 模块 包 if name == main 软件目录结构规范 作业-ATM+购物商城程序 1. 模块 1. 模块导入方法 import 语句 import module1 ...
随机推荐
- x01.os.5: DOS 功能调用
DOS 功能调用(INT 21)-------------------------------AH = 0-2E 适用 DOS 1.0 以上版本AH = 2F-57 适用 DOS 2.0 以上版本AH ...
- vmware 10 e1000e e1000e_cyclecounter_read 挂机解法
http://ehc.ac/p/e1000/mailman/message/34100875/In the e1000e_cyclecounter_read function, if incvalue ...
- shell脚本学习指南
一.UNIX各个工具所支持的正则表达式: 二.使用sed在某一目录下建立一份目录备份: find /home/foo/ -type d -print | #找出/home/foo/目录下所有目录文件 ...
- 【转载国外好文】代工开发一个iOS应用没有那么容易
导读:这是来自新加坡的 iOS 开发者 Kent Nguyen 发表在1月底的一篇博文.这篇吐槽文在 iOS 开发圈子里流传甚广,从原文150多个评论就可见一斑,现翻译如下. 让我们开门见山吧:做一个 ...
- 扫盲 BT Sync——不仅是同步利器,而且是【分布式】网盘
先向大伙儿宣布个好消息-- 经过多位热心读者的大力支持,经过几天的努力,已经完成了"微软网盘"到"BitTorrent Sync"的迁移工作. 再次向这批热心读 ...
- Codeforces Round #283 Div.2 D Tennis Game --二分
题意: 两个人比赛,给出比赛序列,如果为1,说明这场1赢,为2则2赢,假如谁先赢 t 盘谁就胜这一轮,谁先赢 s 轮则赢得整个比赛.求有多少种 t 和 s 的分配方案并输出t,s. 解法: 因为要知道 ...
- VS设置程序集属性(文件的详细信息)
适用范围 本文方法适用于:C#创建的控制台程序,WinForm,WPF等VS创建的.Net工程信息设置. 方法步骤 1.在 项目 上点击鼠标右键选择 属性 ,进入这个页面,点击 程序集信息(重点关注 ...
- JAVA 字符串驻留池
一切从String str = new String("abc")说起... 这行代码形式上很简单,其实很复杂.有一个常见的Java笔试题就是问上面这行代码创建了几个Stri ...
- S2结业考试的第一次测验
错题分析: 1:java中的错误处理是通过异常处理模型来实现的,那么异常处理模块能处理的错误是: A:运行时错误 B:逻辑错误 C:语法错误 D:内部错误 正确答案是:A 解析:运行时异常都是Runt ...
- ubuntu下解析udt数据包
udt是通过udp进行端到端可靠传输的一个协议,有其默认拥塞控制算法. 之前ubuntu下wireshark的版本是1.10,不能直接解析udt数据包[1],升级到最新的2.0.0即可过滤udt数据包 ...