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.
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支持开发的高级重用机制,例 ...
随机推荐
- ActiveX: 如何用.inf和.ocx文件生成cab文件
ActiveX: 如何用.inf和.ocx文件生成cab文件
- 创建 Web 前端开发环境
Web 前端开发涉及多种工具,这里将常用工具的安装和配置进行说明,提供了详细的说明,为后继的开发创建一个坚实的基础. 本文介绍的工具有:NodeJS, NPM, Bower, Git 和 Grunt. ...
- Xamarin Android自学和实践步骤
一.入门(已完成) 1.学习Xamarin Android项目的基本结构 2.学习界面布局的基本方式 3.学习基本编码规则 4.学习页面跳转和传值 5.学习对话框和提示信息显示方法 6.学习使用系统剪 ...
- firefox浏览器不支持复制粘贴(linux)
在Linux主机下使用firefox在线编辑文章时,提示不支持复制粘贴选项,并给出了解决方法,记录一下 1.先找到本机firefox的配置文件的所在文件夹位置,不知道的请遵循以下步骤 点击菜单栏的帮助 ...
- C++ 里大写TRUE和小写true区别
1.C++里大写TRUE和小写true区别 true是bool型的: TRUE是int型的,VC里这个是ms自己定义的: C++规定不允许只通过返回类型不同区别两个函数 2.MFC中的”false“和 ...
- 循序渐进Python3(十一) --5-- 同源策略
一.什么是同源策略 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能.它是由Netscape提出的一个著名的安全策略,现在所有的可支持javascript ...
- LeetCode 214 Shortest Palindrome
214-Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding ch ...
- 【BZOJ1208】宠物收养所(平衡树,splay)
题意:见题面 思路:因为每个时刻要么全是人要么全是宠物,所以可以一棵splay解决 维护单点插入,单点删除,求前驱,求后继即可 ..,..]of longint; num,fa:..]of longi ...
- SpringRMI远程方法调用
Spring为各种远程访问技术的集成提供了工具类. 该小段引用自 http://www.open-open.com/lib/view/open1408957290478.html Spring远程支持 ...
- 简洁的java代码
最近在codewars上刷题,学习到了不少简洁优雅的代码. codewars和leetcode都是刷题网站,codewars的题目难度分类比较广,适合各种不同水平的coder刷题. 刷完题后,看一下其 ...