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.

  • 相关阅读:
    浅析HTT
    html5移动端页面分辨率设置及相应字体大小设置的靠谱使用方式
    CSS--posiion学习日记
    面向对象程序设计课程感想
    OO第二单元(5-8周)作业总结
    OO第一作业周期(前四周)总结
    OO第三阶段作业总结
    OO第二单元作业总结
    Java学习第一阶段作业总结
    面向对象OO课程学习总结
  • 原文地址:https://www.cnblogs.com/xjxz/p/5074990.html
Copyright © 2011-2022 走看看