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.

最新文章

  1. Scrapy创建zentao爬虫
  2. 在ubuntu16编译安装nginx-1.10.2(full)完全自带组件
  3. C#GDI+编程基础(二)
  4. Xperf Analysis Basics(转)
  5. java分别通过httpclient和HttpURLConnection获取图片验证码内容
  6. PHP 数据库驱动、连接数据不同方式学习笔记
  7. Windows8 各种版本区别对比详解
  8. 一站式学习Wireshark(五):TCP窗口与拥塞处理
  9. linux安装rz和sz
  10. kafka web console安装
  11. thinkphp学习笔记4—眼花缭乱的配置
  12. C# 线程的定义和使用
  13. 使用vue-cli构建多页面应用+vux(二)
  14. Spring Boot 之构建Hello Word项目
  15. 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest
  16. Xshell配置使用linux的图形界面
  17. centos 7 上Hive-2.1.1的安装与基本操作
  18. Ext.net GridPanel锁定列需要注意的几个问题
  19. [Day6]引用数据类型、ArrayList 集合
  20. zookerper安装部署

热门文章

  1. 51nod 1020 逆序排列 DP
  2. 如何用九条命令在一分钟内检查Linux服务器性能?
  3. 初识AOP与动态代理
  4. labview生成可执行文件
  5. poj 1318Word Amalgamation
  6. Python爬虫学习之使用beautifulsoup爬取招聘网站信息
  7. admin密码重置方式
  8. 》》mui--图片轮播
  9. 两个linux之间拷贝文件及文件夹
  10. HDU 4162 Shape Number(字符串,最小表示法)