python versions compatibility_为什么在python中迭代一个字典时必须调用.iteritems()?...
Why do you have to call iteritems() to iterate over key, value pairs in a dictionary? ie
dic = {'one':'1', 'two':'2'}
for k, v in dic.iteritems():
print k, v
Why isn't that the default behavior of iterating over a dictionary
for k, v in dic:
print k, v
解决方案
For every python container C, the expectation is that
for item in C:
assert item in C
will pass just fine -- wouldn't you find it astonishing if one sense of in (the loop clause) had a completely different meaning from the other (the presence check)? I sure would! It naturally works that way for lists, sets, tuples, ...
So, when C is a dictionary, if in were to yield key/value tuples in a for loop, then, by the principle of least astonishment, in would also have to take such a tuple as its left-hand operand in the containment check.
How useful would that be? Pretty useless indeed, basically making if (key, value) in C a synonym for if C.get(key) == value -- which is a check I believe I may have performed, or wanted to perform, 100 times more rarely than what if k in C actually means, checking the presence of the key only and completely ignoring the value.
On the other hand, wanting to loop just on keys is quite common, e.g.:
for k in thedict:
thedict[k] += 1
having the value as well would not help particularly:
for k, v in thedict.items():
thedict[k] = v + 1
actually somewhat less clear and less concise. (Note that items was the original spelling of the "proper" methods to use to get key/value pairs: unfortunately that was back in the days when such accessors returned whole lists, so to support "just iterating" an alternative spelling had to be introduced, and iteritems it was -- in Python 3, where backwards compatibility constraints with previous Python versions were much weakened, it became items again).
python versions compatibility_为什么在python中迭代一个字典时必须调用.iteritems()?...相关推荐
- python中怎样向字典中添加值_python中向一个字典中加值
广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 开发准备cas 的 python sdk 包含了用于访问和操作 cas 的所有 ...
- python画50个图-python 实现在一张图中绘制一个小的子图方法
有时候为了直观展现图的信息,可以在大图中添加小子图的方式进行数据分析,如下图所示: 具体的代码如下:该图连接了数据库,当然重要的不是数据展示,而是添加子图的方法. import matplotlib. ...
- python画简便的图-python 实现在一张图中绘制一个小的子图方法
有时候为了直观展现图的信息,可以在大图中添加小子图的方式进行数据分析,如下图所示: 具体的代码如下:该图连接了数据库,当然重要的不是数据展示,而是添加子图的方法. import matplotlib. ...
- 任意长度的python列表最后一个元素索引_任意长度的 Python 列表、元组和字符串中最后一个元素的下标为 ________ 。_学小易找答案...
[填空题]已知 x = [3, 5, 7] ,那么表达式 x[10:] 的值为 ____________ . [填空题]表达式 [x for x in [1,2,3,4,5] if x<3] 的 ...
- python一张图-python 实现在一张图中绘制一个小的子图方法
有时候为了直观展现图的信息,可以在大图中添加小子图的方式进行数据分析,如下图所示: 具体的代码如下:该图连接了数据库,当然重要的不是数据展示,而是添加子图的方法. import matplotlib. ...
- 任意长度的python列表最后一个元素索引_任意长度的Python列表、元组和字符串中最后一个元素的下标为...
[填空题]Python内置函数__用来返回序列中的最小元素 [填空题]表达式 list(range(5)) 的值为 [填空题]表达式 int(4**0.5) 的值为 [填空题]表达式 3 or 5 的 ...
- 任意长度的python列表_任意长度的Python列表、元组和字符串中最后一个元素的索引为...
任意长度的Python列表.元组和字符串中最后一个元素的索引为 答:-1 什么是软件工程? 答:百度定义:软件工程是一门研究用工程化方法构建和维护有效的.实用的和高质量的软件的学科.它涉及程序设计语言 ...
- python处理列表中字典_Python列表嵌套字典的时候,如果要删除列表中其中一个字典要如何操作...
如果要删除列表中其中一个字典要操作如下 第一种方法 1.1 name_list=[{"name":"张三","age":18},{" ...
- python如何将两个list合并成字典_怎么把两个列表合并成字典 论Python怎样将两个list合并为一个字典...
Python怎样把2个列表合成为一个字典? 怎样把2个列表合成一个字典里了?如果你从一开始就选择低下头的话你就可以一直低着头.可是如果你一开始选择了昂着头的话你就永远不能低头了.荣辱说到底只是一瞬间的 ...
最新文章
- php配置mongodb扩展、安装mongodb服务教程
- JQuery 动态创建表单,并自动提交
- websocket 例子
- netcore读取json文件_.Net Core如何读取Json配置文件
- geek软件_社团秀@UNC新媒体协会@管理会计研学社@Geek社团
- SQl---基础整理5
- POJ - 3254 Corn Fields(状压dp)
- Linux上搭建nginx,及简单配置
- wps怎么投递简历发到boss直聘_BOSS直聘角逐招聘季:装机量、下载增量、增长率三料冠军...
- 一步一步学习Servlet输出HelloServlet详解
- sql tempdb清理_SQL Server 2019内存优化的TempDB元数据
- [.net基础]访问修饰符
- Linux 下用yum安装mysql
- 同义词抽取:从知识库中自动发现同义词的方法
- 浅谈LoRa,LoRaWAN,NB-IoT三类物联网技术
- 度分秒怎么计算加减乘除?
- 金融计量模型(十):协整和误差修正模型
- 【程序人生】有个程序员男朋友是什么体验?被公开吐槽
- Android Studio 网络调试
- 安鑫 阿里、腾讯、百度,谁将成为互联网的老大
热门文章
- python tkinter button颜色变不了_更改函数中Tkinter按钮的颜色
- 完全二叉树最小深度_二叉树:我有多少个节点?
- 服务器自动登录 linux,linux下如何自动登录远程服务器?
- python:series一些函数用法
- 终于弄明白 i = i++和 i = ++i 了
- 华为:鸿蒙OS要一统江湖!
- SpringBoot+Mybatis多模块(module)项目搭建教程
- 注意了,Fastjson 最新高危漏洞来袭!
- Android的Handler,Looper源码剖析
- 前端开发工程师做些什么?