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?
xrangeis a sequence object that evaluates lazily.xrangecreates 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.
python代码优化---就喜欢细节的更多相关文章
- python基础===Python 代码优化常见技巧
Python 代码优化常见技巧 代码优化能够让程序运行更快,它是在不改变程序运行结果的情况下使得程序的运行效率更高,根据 80/20 原则,实现程序的重构.优化.扩展以及文档相关的事情通常需要消耗 8 ...
- Python 代码优化常见技巧
代码优化能够让程序运行更快,它是在不改变程序运行结果的情况下使得程序的运行效率更高,根据 80/20 原则,实现程序的重构.优化.扩展以及文档相关的事情通常需要消耗 80% 的工作量.优化通常包含两方 ...
- python代码优化技巧
转自:http://www.douban.com/group/topic/31478102/ 这个资料库还有些不错的好文章: http://www.ibm.com/developerworks/cn/ ...
- python函数的参数细节
按"指针"传递 python中变量赋值.参数传递都是通过"指针"拷贝的方式进行的.除了按"指针"拷贝,还有一种按值拷贝的方式,关于按值.按指 ...
- Python 代码优化技巧(一)
Table of Contents 1. 代码优化Part1 1.1. if 判断的短路特性 1.2. join 合并字符串 1.3. while 1 和 while True 1.4. cProfi ...
- Python代码优化概要
Python即是面向过程语言,也是面向对象语言,很多其它情况下充当脚本语言的角色.虽是脚本语言,但相同涉及到代码优化的问题,代码优化可以让程序执行更快,它是在不改变程序执行结果的情况下使程序执行效率更 ...
- Python代码优化及技巧笔记(一)
前言 这里是记录一些本人在开发过程中遇到的一些细节问题.与君共勉. 版权说明 著作权归作者全部.商业转载请联系作者获得授权,非商业转载请注明出处. 作者:Coding-Naga链接:http://bl ...
- 提高 python 效率的一些细节方式
在列表里面计数 性能:第二种计数方法比第一种快6290倍,为啥因为Python原生的内置函数都是优化过的,所以能用原生的计算的时候,尽量用原生的函数来计算. 过滤一个列表 性能:第二种方法比第一种慢近 ...
- python为什么人们喜欢学习呢?
软件的质和量. 既有量的积累也有质的区别.继承一定的前人研究基础. 基本上来说,python更加的注重可读性,一致性,可移植性,其中软件的质量也是比较的讲究的. python支持开发的高级重用机制,例 ...
随机推荐
- Sql Server中不常用的表运算符之APPLY(2)
在Sql Server中不常用的表运算符之APPLY(1)中提到,SQL2005中新支持的APPLY的特性:1.可以直接将表表达式(表值函数或者子查询)作为APPLY语句的右表连接左表.2.由于使用A ...
- 在eclipse中启动tomcat加载不了项目的解决方法
一.在server视图右键选择Add and Remove时,如果想要部署的项目不在左侧的待选列表中,或是弹出警告There are no resources that can be added or ...
- 需要交互的shell编程——EOF(转载)
在shell编程中,”EOF“通常与”<<“结合使用,“<<EOF“表示后续的输入作为子命令或子shell的输入,直到遇到”EOF“, 再次返回到主调shell,可将其理解为分 ...
- Unity3D 物体移动方式
1. 简介 在Unity3D中,有多种方式可以改变物体的坐标,实现移动的目的,其本质是每帧修改物体的position. 2. 通过Transform组件移动物体 Transform 组件用于描述物体在 ...
- unity3d 射弹基础案例代码分析
#pragma strict import UnityEngine.UI; function Start () { } var speed : int = 5; var newobject : Tra ...
- 关于checkbox复选框
1.复选框后面为什么会有间距,如图 首先这肯定不是空格.实际上是这样的,在Firefox,chrome,Safari等现代浏览器下复选框与文字的间隔确实是由margin引起的,也就是默认情况下,che ...
- re模块汇总
text = 'The Attila the hun show' m = re.match('.',text)#任意单个字符 m.group() 'T' m = re.match('.*',text) ...
- ETL利器Kettle
ETL利器Kettle实战应用解析系列一[Kettle使用介绍] 本系列文章主要索引如下: 一.ETL利器Kettle实战应用解析系列一[Kettle使用介绍] 二.ETL利器Kettle实战应用解析 ...
- .Net 零星小知识
1. 深拷贝和浅拷贝 单纯讲这两个词,其实不容易记住区别,但是看看他们对应的英语单词就显而易见了: 深拷贝: Clone 浅拷贝: Copy 记住了这个,下面在看看详细一点的信息: Copy: 只是复 ...
- 利用target的特性,可以实现纯css的tab效果切换
基础知识: :target起作用的是href连接到的位置 如 <a href="#tab1">tab1</a> <div id="tab1& ...