Views And Iterators Instead Of Lists

Some well-known APIs no longer return lists:

  • dict methods dict.keys()dict.items() and dict.values() return “views” instead of lists. For example, this no longer works: k = d.keys(); k.sort(). Use k = sorted(d) instead (this works in Python 2.5 too and is just as efficient).

  • Also, the dict.iterkeys()dict.iteritems() and dict.itervalues() methods are no longer supported.

  • map() and filter() return iterators. If you really need a list and the input sequences are all of equal length, a quick fix is to wrap map() in list(), e.g. list(map(...)), but a better fix is often to use a list comprehension (especially when the original code uses lambda), or rewriting the code so it doesn’t need a list at all. Particularly tricky is map() invoked for the side effects of the function; the correct transformation is to use a regular for loop (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 with map() from Python 2.x, also wrap the sequences initertools.zip_longest(), e.g. map(func, *sequences) becomes list(map(func,itertools.zip_longest(*sequences))).

  • range() now behaves like xrange() 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的更多相关文章

  1. Python 高度定制化自己的线程类和进程类代码,获取启动进程或线程方法的结果(兼容Py2和Py3)

    #encoding=utf-8 from threading import Thread from multiprocessing import Process import multiprocess ...

  2. python模块之lib2to3(py2转py3自动化工具)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python模块之lib2to3(py2转py3自动化工具) #http://tieba.baidu.com ...

  3. py2与py3区别总结

    1. py2中的str是py3中的bytes py2中的Unicode是py3中的str 声明一个字符串变量时,py2 和py3都是str类型,但py2代表字节类型,py3代表文本类型 隐式转换: p ...

  4. windows10上同时安装py2和py3的情况

    2018-06-14  16:14:51 1.同时安装python2和python3的时候,pip只是其中一个版本,使用对应Python版本的pip时,在命令行分别输入如下命令: 查看不同Python ...

  5. Py2与Py3的区别

    总结Py2 与Py3 的区别 1 编码区别 在Python2中有两种字符串类型str和Unicode. 默认ASCII python2 str类型,相当于python3中的bytes类型 python ...

  6. 浅谈Python内置对象类型——数字篇(附py2和py3的区别之一)

    Python是一门面向对象的编程设计语言,程序中每一样东西都可以视为一个对象.Python内置对象可以分为简单类型和容器类型,简单类型主要是数值型数据,而容器类型是可以包含其他对象类型的集体,如序列. ...

  7. python py2与py3的编码问题

    一:什么是编码 将明文转换为计算机可以识别的编码文本称为"编码".反之从计算机可识别的编码文本转回为明文为"解码". 那么什么是明文呢,首先我们从一段信息说起, ...

  8. [PY3]——对iterator的处理(解析式、map、reduce、filter)

    引言 对iterator一般可以用for in方法处理,但有时可以借助更高效.也更易读的方式去处理. 例如解析式(包括列表解析式.生成器解析式.集合解析式.字典解析式), 例如map( ).reduc ...

  9. py2和py3之间的不同

    1.print函数 很琐碎,而 print 语法的变化可能是最广为人知的了,但是仍值得一提的是: Python 2 的 print 声明已经被 print() 函数取代了,这意味着我们必须包装我们想打 ...

随机推荐

  1. openstack pike 单机 一键安装 shell

    #openstack pike 单机  centos 一键安装 shell #openstack pike 集群高可用  安装部署 汇总 http://www.cnblogs.com/elvi/p/7 ...

  2. Python函数篇(2)-递归函数、匿名函数及高阶函数

    1.全局变量和局部变量 一般定义在程序的最开始的变量称为函数变量,在子程序中定义的变量称为局部变量,可以简单的理解为,无缩进的为全局变量,有缩进的是局部变量,全局变量的作用域是整个程序,而局部变量的作 ...

  3. caioj 1236 最近公共祖先 树倍增算法模版 倍增

    [题目链接:http://caioj.cn/problem.php?id=1236][40eebe4d] 代码:(时间复杂度:nlogn) #include <iostream> #inc ...

  4. mysql 各数据类型的 大小及长度

    数字型 类型 大小 范围(有符号) 范围(无符号) 用途 TINYINT 1 字节 (-128,127) (0,255) 小整数值 SMALLINT 2 字节 (-32 768,32 767) (0, ...

  5. JavaScript学习总结(一)——ECMAScript、BOM、DOM(核心、浏览器对象模型与文档对象模型)

    一.JavaScript简介 JavaScript是一种解释执行的脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型,它遵循ECMAScript标准.它的解释器被称为JavaScript引 ...

  6. springMVC使用jsp:include嵌入页面的两种方式

    1.静态嵌入子页面 <%@ include file="header.jsp" %>   静态嵌入支持 jsp . html . xml 以及纯文本. 静态嵌入在编译时 ...

  7. day5、文件乱码怎么解决

    1.1 Linux下,如何将一个乱码的文件进行重命名 方法一: 命令格式:mv $(ls   |egrep "[^a-zA-Z0-9.-]") tandao.tx [root@nb ...

  8. backbone与require的共存问题解决

    如果向下面那样直接列出script标签可能会出现错误,   <script type="text/javascript" src="/dep/jquery-1.11 ...

  9. Codeforces 29D Ant on the Tree 树的遍历 dfs序

    题目链接:点击打开链接 题意: 给定n个节点的树 1为根 则此时叶子节点已经确定 最后一行给出叶子节点的顺序 目标: 遍历树并输出路径.要求遍历叶子节点时依照给定叶子节点的先后顺序訪问. 思路: 给每 ...

  10. 再谈 SharePoint 大局观

    作者:陈希章 发表于 2017年12月21日 前言 我对SharePoint这个产品很有感情,因为曾经有相当长一段时间,在很多个夜深人静.月黑风高的晚上,我都是在和它形影不离,在一个一个项目实践中相爱 ...