zoukankan      html  css  js  c++  java
  • python代码优化---就喜欢细节

    地址:http://www.codeproject.com/Tips/829060/Python-Code-Optimizations-Part-One

    转发过来保存一下。喜欢精雕细琢,编程才有乐趣。作者牛。

     

    Introduction

    Listed below are some very common day to day usage scenarios, and a way to do them pythonically!

    Using the Code

    1. Looping over a Range of Numbers

    for i in [0,1,2,3,4,5]:
        print i**2
    Better way (looks better):
    for i in range(6):
        print i**2

    What Happens in this Loop?

    range produces a list in memory and then the for loop loops over the list.
    Both create a list of 6 integers in memory and then iterate over each number, raise it to power 2 and then print. Thus, both the loops do exactly the same thing in exactly the same way!

    Pythonic way: Use xrange()
    #Python 2.x
    for i in xrange(6):
        print i**2
    
    #Python 3.x
    for i in range(6):
        print i**2

    What is xrange?

    • xrange is a sequence object that evaluates lazily.
    • xrange creates an iterator over the range(list) and yields one number at a time, thus consuming less amount of memory than the methods above.

    2. Looping Over a Collection

    colours = ['red', 'green', 'blue', 'yellow']
    
    for i in range(len(colours)):
        print colours[i]
    Pythonic way
    for colour in colours:
        print colour

    3. Looping Over a Collection and its Indices

    for i in range(len(colours)):
        print i, '-->', colours[i]
    Pythonic way: use enumerate()
    for i, colour in enumerate(colours):
        print i, '-->', colour

    4. Looping Backwards

    for i in range(len(colours), -1, -1, -1):
        print colours[i]
    Pythonic way: Use reversed()
    for colour in reversed(colours):
        print colour

    5. Loooping in Sorted Order

    Pythonic way: Use sorted()
    for colour in sorted(colours):
        print colour

    Looping Backwards in Sorted Order

    Just add reverse=True to the sorted function arguments list.

    Pythonic Way
    for colour in sorted(colours, reverse=True):
        print colour

    6. Looping Over Two Collections

    names = ['a', 'b', 'c']
    colours = ['red', 'green', 'blue', 'yellow']
    
    n = min(len(colours), len(names))
    
    for i in range(n):
        print names[i], '-->', colours[i]
    Better Way
    for name, colour in zip(names, colours):
        print name, '-->', colour    

    zip creates a third list in memory which consists of tuples, each of which is its own separate object with pointers back to the original. In other words, it takes far more memory than the original two lists combined.
    Most importantly "It doesn't scale!".

    Pythonic Way: use izip()
    from itertools import izip
    for name, colour in izip(names, colours):
        print name, '-->', colour

    For smaller lists, zip is faster, but if you have lists with millions of records, then better use izip, as izip only advances the underlying iterators when needed.

  • 相关阅读:
    Atitit  Uncaught (in promise) SyntaxError Unexpected token < in JSON at position 0
    Atitit  验证 数字验证 非空验证的最佳算法  h5
    Atitit 转移特效attilax总结
    Atitit Loading 动画效果
    Atitit 项目管理 提升开发效率的项目流程方法模型 哑铃型  橄榄型 直板型
    Atitit hibernate3 hinernate4 hibernate5新特性attilax总结
    Atitit js es5 es6新特性 attilax总结
    Atitit mybatis 3 3.2 3.3  3.4 新特性attilax总结
    Atitit spring 3.0 3.1 3.2 4.0 4.3 5.0 新特性
    Atitit jquery  1.4--v1.11  v1.12  v2.0  3.0 的新特性
  • 原文地址:https://www.cnblogs.com/xjxz/p/5074990.html
Copyright © 2011-2022 走看看