变量交换
>>> a, b = b, a
循环遍历区间元素
>>>for i in range(10):
... print (i)
返回的是生成器对象,生成器比列表更加节省内存 带索引位置的循环遍历
>>>colors = ['red', 'green', 'blue', 'yellow'] >>>for i, color in enumerate(colors):
... print (i, 'mapping', color)
字符串连接
>>>colors = ['red', 'green', 'blue', 'yellow']
>>>print( ', ', join( colors ) )
join 是一种更加高效的字符串连接方式,使用 + 操作时,每执行一次+操作就会导致在内存中生成一个新的字符串对象,遍历几次有几个字符串生成,造成无谓的内存浪费。而用 join 方法整个过程只会产生一个字符串对象。 打开/关闭文件
>>>with open('data.txt') as f:
... data = f.read()
列表推导式
result = []
for i in range( 10 ):
s = i * 2
result.append( s ) 应该写成: [ i * 2 for i in range( 10 ) ]
1
装饰器 装饰器可以把与业务逻辑无关的代码抽离出来,让代码保持干净清爽,而且装饰器还能被多个地方重复利用。比如一个爬虫网页的函数,如果该 URL 曾经被爬过就直接从缓存中获取,否则爬下来之后加入到缓存,防止后续重复爬取。 def web_lookup(url, saved={}):
if url in saved:
return saved[url]
page = urllib.urlopen(url).read()
saved[url] = page
return page pythonic import urllib.request def cache(func):
saved = {} def wrapper(url):
if url in saved:
return saved[url]
else:
page = func(url)
saved[url] = page
return page
return wrapper @cache
def web_lookup(url):
return urllib.urlopen(url).read()
列表的操作
列表对象(list)是一个查询效率高于更新操作的数据结构,比如删除一个元素和插入一个元素时执行效率就非常低,因为还要对剩下的元素进行移动 names = ['raymond', 'rachel', 'matthew', 'roger',
'betty', 'melissa', 'judith', 'charlie']
names.pop(0)
names.insert(0, 'mark')
pythonic from collections import deque
names = deque(['raymond', 'rachel', 'matthew', 'roger',
'betty', 'melissa', 'judith', 'charlie'])
names.popleft()
names.appendleft('mark')
deque 是一个双向队列的数据结构,删除元素和插入元素会很快 序列解包
p = 'vttalk', 'female', 30, 'python@qq.com' name = p[0]
gender = p[1]
age = p[2]
email = p[3]
pythonic name, gender, age, email = p

pythonic operations的更多相关文章

  1. backup, file manipulation operations (such as ALTER DATABASE ADD FILE) and encryption changes on a database must be serialized.

    昨天在检查YourSQLDba备份时,发现有台数据库做备份时出现了下面错误信息,如下所示: <Exec>   <ctx>yMaint.ShrinkLog</ctx> ...

  2. HDU 5938 Four Operations(四则运算)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  3. ios基础篇(二十九)—— 多线程(Thread、Cocoa operations和GCD)

    一.进程与线程 1.进程 进程是指在系统中正在运行的一个应用程序,每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内: 如果我们把CPU比作一个工厂,那么进程就好比工厂的车间,一个工厂有 ...

  4. 一些Python的惯用法和小技巧:Pythonic

    Pythonic其实是个模糊的含义,没有确定的解释.网上也没有过多关于Pythonic的说明,我个人的理解是更加Python,更符合Python的行为习惯.本文主要是说明一些Python的惯用法和小技 ...

  5. OpenCascade Modeling Algorithms Boolean Operations

    Modeling Algorithms Boolean Operations of Opencascade eryar@163.com 布尔操作(Boolean Operations)是通过两个形状( ...

  6. A.Kaw矩阵代数初步学习笔记 4. Unary Matrix Operations

    “矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔 ...

  7. A.Kaw矩阵代数初步学习笔记 3. Binary Matrix Operations

    “矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔 ...

  8. mouse scrollings and zooming operations in linux & windows are opposite

    mouse scrollings and zooming operations in linux & windows are opposite. windows中, 鼠标滚动的方向是: 查看页 ...

  9. MongoDB—— 写操作 Core MongoDB Operations (CRUD)

    MongoDB使用BSON文件存储在collection中,本文主要介绍MongoDB中的写操作和优化策略. 主要有三种写操作:        Create        Update        ...

随机推荐

  1. 微信小程序生成指定页面小程序码海报图片分享思路总结

    本博客主要说下思路,具体代码不贴 1.考虑到组件复用,所以我把它做成一个自定义的组件 <my-poster id="getPoster" avater="{{ima ...

  2. 浪院长 | spark streaming的使用心得

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/rlnLo2pNEfx9c/article/details/82505159 今天.主要想聊聊spar ...

  3. 自动化web前端测试,自动登录网站.目前发现最靠谱的方法是imacros

    imacros免费版 登录宏代码的示例: //首先登出URL GOTO=http://yoursite/logout.html//打开登录页面URL GOTO=http://yoursite/logi ...

  4. bootstrap table 的简单Demo

    暂时够用,不够用再补充 T_T script: <link rel="stylesheet" href="lib/bootstrap.min.css"&g ...

  5. 获取当前网页的绝对URL地址

    通过创建一个虚拟的<a></a>元素,将它的href指定为相对URL,再读取它的href就会得到绝对URL. var getAbsoluteUrl = (function() ...

  6. Chrome 控制台报错Unchecked runtime.lastError: The message port closed before a response was received

    Chrome浏览器控制台报错提示 Unchecked runtime.lastError: The message port closed before a response was received ...

  7. IE 下js里面new Date("2017-07-11 08:00:00") 出现NAN的问题以及解决方法

    在js里面用了这个方法   var  $date= new Date("2017-07-11 08:00:00") 可是打印的时候为 NAN.查了下  只有IE下有这个问题,然后我 ...

  8. 【转载】Linux 命令行快捷键 - 移动光标

    Linux 命令行快捷键 - 移动光标 涉及在linux命令行下进行快速移动光标.命令编辑.编辑后执行历史命令.Bang(!)命令.控制命令等.让basher更有效率. 常用 ctrl+左右键:在单词 ...

  9. 无法加载协定为“ServiceReference1.xxxxxx”的终结点配置部分,因为找到了该协定的多个终结点配置。请按名称指示首选的终结点配置部分。

    原因是在web.config 文件中多次引用了“添加外部引用” <system.serviceModel> <bindings> <basicHttpBinding> ...

  10. surface shader获取像素深度差值

    void vert (inout appdata_full v, out Input i) { UNITY_INITIALIZE_OUTPUT(Input, i); i.proj = ComputeS ...