py2 to py3 return iterator
Views And Iterators Instead Of Lists
Some well-known APIs no longer return lists:
dictmethodsdict.keys(),dict.items()anddict.values()return “views” instead of lists. For example, this no longer works:k = d.keys(); k.sort(). Usek = sorted(d)instead (this works in Python 2.5 too and is just as efficient).Also, the
dict.iterkeys(),dict.iteritems()anddict.itervalues()methods are no longer supported.map()andfilter()return iterators. If you really need a list and the input sequences are all of equal length, a quick fix is to wrapmap()inlist(), e.g.list(map(...)), but a better fix is often to use a list comprehension (especially when the original code useslambda), or rewriting the code so it doesn’t need a list at all. Particularly tricky ismap()invoked for the side effects of the function; the correct transformation is to use a regularforloop (since creating a list would just be wasteful).If the input sequences are not of equal length,
map()will stop at the termination of the shortest of the sequences. For full compatibility withmap()from Python 2.x, also wrap the sequences initertools.zip_longest(), e.g.map(func, *sequences)becomeslist(map(func,itertools.zip_longest(*sequences))).range()now behaves likexrange()used to behave, except it works with values of arbitrary size. The latter no longer exists.zip()now returns an iterator.
py2 to py3 return iterator的更多相关文章
- Python 高度定制化自己的线程类和进程类代码,获取启动进程或线程方法的结果(兼容Py2和Py3)
#encoding=utf-8 from threading import Thread from multiprocessing import Process import multiprocess ...
- python模块之lib2to3(py2转py3自动化工具)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python模块之lib2to3(py2转py3自动化工具) #http://tieba.baidu.com ...
- py2与py3区别总结
1. py2中的str是py3中的bytes py2中的Unicode是py3中的str 声明一个字符串变量时,py2 和py3都是str类型,但py2代表字节类型,py3代表文本类型 隐式转换: p ...
- windows10上同时安装py2和py3的情况
2018-06-14 16:14:51 1.同时安装python2和python3的时候,pip只是其中一个版本,使用对应Python版本的pip时,在命令行分别输入如下命令: 查看不同Python ...
- Py2与Py3的区别
总结Py2 与Py3 的区别 1 编码区别 在Python2中有两种字符串类型str和Unicode. 默认ASCII python2 str类型,相当于python3中的bytes类型 python ...
- 浅谈Python内置对象类型——数字篇(附py2和py3的区别之一)
Python是一门面向对象的编程设计语言,程序中每一样东西都可以视为一个对象.Python内置对象可以分为简单类型和容器类型,简单类型主要是数值型数据,而容器类型是可以包含其他对象类型的集体,如序列. ...
- python py2与py3的编码问题
一:什么是编码 将明文转换为计算机可以识别的编码文本称为"编码".反之从计算机可识别的编码文本转回为明文为"解码". 那么什么是明文呢,首先我们从一段信息说起, ...
- [PY3]——对iterator的处理(解析式、map、reduce、filter)
引言 对iterator一般可以用for in方法处理,但有时可以借助更高效.也更易读的方式去处理. 例如解析式(包括列表解析式.生成器解析式.集合解析式.字典解析式), 例如map( ).reduc ...
- py2和py3之间的不同
1.print函数 很琐碎,而 print 语法的变化可能是最广为人知的了,但是仍值得一提的是: Python 2 的 print 声明已经被 print() 函数取代了,这意味着我们必须包装我们想打 ...
随机推荐
- Linux EXT 文件系统 详解
上几章我们讲到了Linux启动的一些问题,接下来我们来看一下硬盘分割和EXT格式文件系统的问题.前面提到了分区表的问题,分区表位于MBR, 占用64个字节.所谓的硬盘分区也就是对硬盘进行规划,填写分区 ...
- tomcat警告setting property 'debug' to '0' did not find a matching property
在使用tomcat6.0版本结合myeclipse进行java web项目,运行程序显示setting property 'debug' to '0' did not find a matching ...
- svn 如果遇到an unversioned directory of the same name already exists的解决办法
svn 如果遇到an unversioned directory of the same name already exists的解决办法 一般这种情况是因为,在一个已经存在的文件夹里做SVN che ...
- Centos下安装git的web服务器
直接上代码 [Shell] 纯文本查看 复制代码 ? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2 ...
- PHP 八种基本的数据类型小结
四种标量类型: boolean (布尔型) integer (整型) float (浮点型, 也称作 double) string (字符串) 两种复合类型: array (数组) object (对 ...
- python学习之路day2
模块学习: 标准库: os: 第三方库:
- 开源:Sagit.Framework For IOS 开发框架
一:创造Sagit开发框架的起因: 记得IT连创业刚进行时,招了个IOS的女生做开发,然后: ----------女生的事故就此开始了----------- 1:面试时候:有作品,态度也不错,感觉应该 ...
- 【NOIP2015提高组】运输计划
https://daniu.luogu.org/problem/show?pid=2680 使完成所有运输计划的时间最短,也就是使时间最长的运输计划耗时最短.最大值最小问题考虑用二分答案,每次chec ...
- Web、WCF和WS通过Nginx共享80端口
团队中的一个Web项目面对的用户网络环境多是在严格的防火墙安全条件下,通常只开放一些标准的端口如80,21等. 上线初期,因忽略了这个问题,除了Web应用是以80端口提供访问外,WCF和WS是以其他端 ...
- 具体解释Java虚拟机类载入
概述 在Java语言里面,类型的载入.连接和初始化过程都是在程序运行期间完毕的.虚拟机把描写叙述类的数据从Class文件或其他地方载入到内存,并对数据进行校验.转换解析和初始化,终于形成能够被虚拟机直 ...