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 数据库的维护(四)__游标(cursor)
--维护数据库-- --游标(cursor)-- --概述: 注:使用select语句查询结果的结果集是一个整体,如果想每次处理一行或一部分行数据,游标可以提供这种处理机制.可以将游标理解为指针.指针 ...
- .NET Core Web 应用部署到 Docker 中运行
环境介绍 : 虚拟机:VirtualBox 5.1.6 系 统:Ubuntu 16.04.1 LTS 系统准备完成后可以使用 sudo apt-get udpate 和 sudo apt-get up ...
- 【NoSql】MongoDb
[NoSql]MongoDb 一. 文档 1. 官网 2. C# Driver 3. C# 开发文档 二. 命令 1. --config "C:\mongodb\mongod.cfg&quo ...
- OC对象的归档及解档浅析
一般用在用户登录,保存这个用户的信息 对象归档,就是把内存中对象持久化. 对象解档,就是把持久化的对象读取到内存. oc中对象归档解档大致分为以下几种方法: 从数量上可以分为: 对单个对象归档解档 对 ...
- thinkphp 添加 修改删除
在 MainController.class.php 添加 public function zhuCe() { //时间两个逻辑 // 1 显示页面 2向数据库添加 if(empty($_POST)) ...
- arduino编程语言Wiring参考手册API
对Arduino的编程是利用 Arduino编程语言 (基于 Wiring)和Arduino开发环境(based on Processing)来实现的. 通过编程,Arduino可以实现很多种功能. ...
- modelsim搭建uvm环境及实例
Modelsim SE-64 10.2c 自带编译好的uvm-1.1d 脚本new_run.do set UVM_DPI_HOME C:/modeltech64_10.2c/uvm-1.1d/win6 ...
- nginx 隐藏版本信息
隐藏nginx头部 修改后的src/core/nginx.h ,代码如下: /* * Copyright (C) Igor Sysoev * Copyright (C) Nginx, Inc ...
- tomcat 配置ssi
tomcat想要使用html文件引入html文件的功能可以开启ssi,这样就可以在html文件中使用include指令,从而使得html文件可以动态加载html文件了. 开启ssi步骤如下: 1.ap ...
- CoordinatorLayout+TabLayout+ViewPager
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.C ...