• py2 to py3 return iterator


    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: d.keys(); k.sort(). Use 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.

  • 相关阅读:
    GDI+绘制字体显示不全
    vector赋值
    创建不响应(不激活)窗口
    MouseHover
    duilib窗口从任务栏恢复问题
    java 加解密
    maven依赖仲裁
    $.ajax()
    mybatis 动态SQL
    Json学习
  • 原文地址:https://www.cnblogs.com/earendil/p/8026765.html
Copyright © 2020-2023  润新知