30 个 Python 语言的特点技巧
1 介绍从我开始学习Python时我就决定维护一个经常使用的“窍门”列表。不论何时当我看到一段让我觉得“酷,这样也行!”的代码时(在一个例子中、在StackOverflow、在开源码软件中,等等),我会尝试它直到理解它,然后把它添加到列表中。这篇文章是清理过列表的一部分。如果你是一个有经验的Python程序员,尽管你可能已经知道一些,但你仍能发现一些你不知道的。如果你是一个正在学习Python的C、C++或Java程序员,或者刚开始学习编程,那么你会像我一样发现它们中的很多非常有用。 每个窍门或语言特性只能通过实例来验证,无需过多解释。虽然我已尽力使例子清晰,但它们中的一些仍会看起来有些复杂,这取决于你的熟悉程度。所以如果看过例子后还不清楚的话,标题能够提供足够的信息让你通过Google获取详细的内容。 |
|
列表按难度排序,常用的语言特征和技巧放在前面。 1.1 分拆 >>> a, b, c = 1, 2, 3 1.2 交换变量分拆 >>> a, b = 1, 2 1.3 拓展分拆 (Python 3下适用) >>> a, *b, c = [1, 2, 3, 4, 5] 1.4 负索引 >>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 1.5 列表切片 (a[start:end]) >>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 1.6 使用负索引的列表切片 >>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 1.7 带步进值的列表切片 (a[start:end:step]) >>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 1.8 负步进值得列表切片 >>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 1.9 列表切片赋值 >>> a = [1, 2, 3, 4, 5] 1.10 命名切片 (slice(start, end, step)) >>> a = [0, 1, 2, 3, 4, 5] 1.11 zip打包解包列表和倍数 >>> a = [1, 2, 3] 1.12 使用zip合并相邻的列表项 >>> a = [1, 2, 3, 4, 5, 6] >>> group_adjacent = lambda a, k: zip(*([iter(a)] * k)) >>> zip(a[::2], a[1::2]) >>> zip(a[::3], a[1::3], a[2::3]) >>> group_adjacent = lambda a, k: zip(*(a[i::k] for i in range(k))) 1.13 使用zip和iterators生成滑动窗口 (n -grams) >>> from itertools import islice 1.14 使用zip反转字典 >>> m = {'a': 1, 'b': 2, 'c': 3, 'd': 4} 1.15 摊平列表: >>> a = [[1, 2], [3, 4], [5, 6]] >>> sum(a, []) >>> [x for l in a for x in l] >>> a = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] >>> a = [1, 2, [3, 4], [[5, 6], [7, 8]]] 注意: 根据Python的文档,itertools.chain.from_iterable是首选。 1.16 生成器表达式 >>> g = (x ** 2 for x in xrange(10)) 1.17 迭代字典 >>> m = {x: x ** 2 for x in range(5)} >>> m = {x: 'A' + str(x) for x in range(10)} 1.18 通过迭代字典反转字典 >>> m = {'a': 1, 'b': 2, 'c': 3, 'd': 4} 1.19 命名序列 (collections.namedtuple) >>> Point = collections.namedtuple('Point', ['x', 'y']) 1.20 命名列表的继承: >>> class Point(collections.namedtuple('PointBase', ['x', 'y'])): 1.21 集合及集合操作 >>> A = {1, 2, 3, 3} 1.22 多重集及其操作 (collections.Counter) >>> A = collections.Counter([1, 2, 2]) 1.23 迭代中最常见的元素 (collections.Counter) >>> A = collections.Counter([1, 1, 2, 2, 3, 3, 3, 3, 4, 5, 6, 7]) 1.24 双端队列 (collections.deque) >>> Q = collections.deque() 1.25 有最大长度的双端队列 (collections.deque) >>> last_three = collections.deque(maxlen=3) 1.26 字典排序 (collections.OrderedDict) >>> m = dict((str(x), x) for x in range(10)) 1.27 缺省字典 (collections.defaultdict) >>> m = dict() 1.28 用缺省字典表示简单的树 >>> import json (到https://gist.github.com/hrldcpr/2012250查看详情) >>> import itertools, collections 1.30 最大最小元素 (heapq.nlargest和heapq.nsmallest) >>> a = [random.randint(0, 100) for __ in xrange(100)] 1.31 笛卡尔乘积 (itertools.product) >>> for p in itertools.product([1, 2, 3], [4, 5]): 1.32 组合的组合和置换 (itertools.combinations 和 itertools.combinations_with_replacement) >>> for c in itertools.combinations([1, 2, 3, 4, 5], 3): 1.33 排序 (itertools.permutations) >>> for p in itertools.permutations([1, 2, 3, 4]): 1.34 链接的迭代 (itertools.chain) >>> a = [1, 2, 3, 4] 1.35 按给定值分组行 (itertools.groupby) >>> from operator import itemgetter >>> print_data(data) >>> data.sort(key=itemgetter(-1)) |
http://www.oschina.net/translate/thirty-python-language-features-and-tricks-you-may-not-know
dd
30 个 Python 语言的特点技巧的更多相关文章
- 你可能不知道的 30 个 Python 语言的特点技巧
列表按难度排序,常用的语言特征和技巧放在前面. 1.1 分拆 >>> a, b, c = 1, 2, 3>>> a, b, c(1, 2, 3)> ...
- 你可能不知道的30个Python语言的特点技巧
1 介绍 从我开始学习Python时我就决定维护一个经常使用的“窍门”列表.不论何时当我看到一段让我觉得“酷,这样也行!”的代码时(在一个例子中.在StackOverflow.在开源码软件中,等等), ...
- 如何玩转Python? 一文总结30种Python的窍门和技巧
Python作为2019年必备语言之一,展现了不可替代作用.对于所有的数据科学工作者,如何提高使用Python的效率,这里,总结了30种Python的最佳实践.技巧和窍门.希望这些可以帮助大家在202 ...
- Python之几个技巧特点
今天偶然看到一篇文章<你可能不知道的30个Python语言的提点技巧>,虽然做python有几年了,但中间还是好多不知道或没想到,特在这里做下摘抄. 原文地址: http://soft.c ...
- 使用 Mypy 检查 30 万行 Python 代码,总结出 3 大痛点与 6 个技巧!
作者:Charlie Marsh 译者:豌豆花下猫@Python猫 英文:Using Mypy in production at Spring (https://notes.crmarsh.com/u ...
- Python语言防坑小技巧
Python语言防坑小技巧 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.赋值即定义 1>.运行以下代码会出现报错 #!/usr/bin/env python #_*_ ...
- [转] Python 代码性能优化技巧
选择了脚本语言就要忍受其速度,这句话在某种程度上说明了 python 作为脚本的一个不足之处,那就是执行效率和性能不够理想,特别是在 performance 较差的机器上,因此有必要进行一定的代码优化 ...
- Python 代码性能优化技巧
选择了脚本语言就要忍受其速度,这句话在某种程度上说明了 python 作为脚本的一个不足之处,那就是执行效率和性能不够理想,特别是在 performance 较差的机器上,因此有必要进行一定的代码优化 ...
- Python 语言基础
Python 语言基础 Python 开发环境 计算机组成 编程语言(计算机语言)是人们为了控制计算机,而设计的一种符号和文字的组合,从而实现向计算机发出指令. 形式是符号和文字的组合 目的是为了控制 ...
随机推荐
- Java多线程Thread.yield(),thread.join(), Thread.sleep(200),Object.wait(),Object.notify(),Object.notifyAll()的区别
Thread.yield(),在某个线程里调用Thread.yield(),会使这个线程由正在运行的running状态转变为等待cpu时间片的runable状态.join()是Thread类的一个非s ...
- Clairewd’s message - HDU 4300(next[]运用)
题目大意:给两个串第一个串是翻译表(密文可以通过翻译表翻译成明文),第二个串是由密文+明文组成,前面是密文(完整的),后面是明文(未必完整),问能不能把第二个串补全,输出最短的一种可能. 分析:题 ...
- 对Spring的一些理解
最近在复习一些关于框架的概念性问题,主要是为了最近的面试,怕被问到这些概念性的问题.不过在真正做开发的时候还是要好好理解这些框架的基本原理,以及它们的工作流程.这样,我们才能更好的使用这些框架.下面就 ...
- Apache Kylin
日前,eBay公司隆重宣布已经正式向开源业界推出分布式分析引擎:Kylin(http://kylin.io).作为一套旨在对Hadoop环境下分析流程进行加速.且能够与SQL兼容性工具顺利协作的解决方 ...
- sql服务器内部参数使用详情(存储过程)
exec sp_help;返回当前数据库中的所有存储过程.exec sp_help datebase.dbo.table名称 返回当前表中的所有对象.如字段名称等.这个吊exec sp_helpfil ...
- Sublime 注册码
----- BEGIN LICENSE ----- Andrew Weber Single User License EA7E-855605 813A03DD 5E4AD9E6 6C0EEB94 BC ...
- android.view.WindowLeaked解决办法
08-07 14:51:28.129: E/WindowManager(22277): Activity com.xxx.xxx.xxx.xxx.LoginActivity has leaked wi ...
- js中return false,return,return true的使用方法及区别
起首return作为返回keyword,他有下面两种返回体式格式 1.返回把握与函数成果 语法为:return 表达式; 语句停止函数履行,返回调用函数,而且把表达式的值作为函数的成果 2.返回把握无 ...
- 《Android开发艺术探索》读书笔记 (6) 第6章 Android的Drawable
本节和<Android群英传>中的第六章Android绘图机制与处理技巧有关系,建议先阅读该章的总结 第6章 Android的Drawable 6.1 Drawable简介 (1)Andr ...
- gridview添加header
gridview是不能添加header的,这里的解决方法是将listview改造成gridview使用,功能很好用,唯一的缺点是列数不能自适应 示例代码下载地址http://pan.baidu.com ...