30个关于Python的你有所不知语言特效和技巧
By Sahand Saba 翻译 Liam
Introduction
自从我开始学习 python,我决定记录下一系列经常用的技巧。当我看到一些代码(例如,在 Overflow
,或者一些开源的应用等等)我觉得很酷,我觉得我们应该不知道这一些用法。当我理解这些代码的时候,我会测试这些代码,然后加入这个列表中。下面发表的是一部分整理后的列表。假如你是一个很专业的 Python
工程师,即使你已经知道大部分用法,你还是可以找到一些你不知道的。假如你是 C, C++, Java 工程师,并且最近想学 Python,或者最近想拓展新的编程方式,那么你将会发现有一部分真的很有用,就想我一样。每一个技巧和语言的特性都将会通过例子演示,而没有解释。同时,我将会尽量使例子简洁明了,可能有一些仍然讲的不清楚,因为专业水平不够。如果有一些例子看完之后还是不能明白的话,你可以利用例子的标题去 Google
找到更多有用的信息。例子大致的按难度排序,一些比较简单和常见的功能和技巧会放在前面。内容的目录我将会放在最后
更新 - March 14th, 2014
Roy Keyes 提了一个非常好的建议,把这个文章放在 github 上,让人们通过提 PR 方式来增加内容。这个仓库是 https://github.com/sahands/python-by-exampl。欢迎大家 fork,提 pull requests。我会更新这篇文章的,当有仓库跟新的时候。
更新 - March 8th, 2014
这篇文章在 Reddit, Hacker News 有很多的讨论, 在那些评论中,很多读者提了很多建议和修改。我已经更新了我的列表,根据改进的建议,并且增加了一些新的条目。我现在确实有这样的一瞬间。”Cool! I didn’t know you could do that!” 另外,我不太清楚 itertools.chain.from_iterable
和 dictionary comprehensions
. 同时,有一些十分有趣的讨论关于是否有一些技术导致难以调试代码的可能性 就我而言,下面的条目本质上是没有很难调试的。但是我可以断定,如果代码写的太长,将会增加调试的难度,以及更加难理解和维护。根据你的判断,如果你的代码足够的简洁,那是有那好维护和阅读。举例来说,我觉得列表推导式可以很好阅读,而且易于调试和维护。但是如果列表推导式在另一个列表推导式中来传递给映射,然后到 itertools.chain
?可能不是好主意!
1.1 Unpacking
1, 2, 3 a, b, c = |
1.2 Unpacking for Swapping Variables
1, 2 a, b = |
1.3 Extended Unpacking (Python 3 only)
1, 2, 3, 4, 5] a, *b, c = [ |
1.4 Negative Indexing
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a = [ |
1.5 List Slices (a[start:end])
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a = [ |
1.6 List Slices with Negative Indexing
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a = [ |
1.7 List Slices with step (a[start:end:step])
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a = [ |
1.8 List Slices with Negative step
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a = [ |
1.9 List Slice Assignment
1, 2, 3, 4, 5] a = [ |
1.10 Naming Slices (slice(start, End, step))
0, 1, 2, 3, 4, 5] a = [ |
1.11 Zipping and Unzipping Lists and Iterables
1, 2, 3] a = [ |
1.12 Grouping Adjacent List Items Using Zip
1, 2, 3, 4, 5, 6] a = [ |
1.13 Inverting a Dictionary Using Zip
'a': 1, 'b': 2, 'c': 3, 'd': 4} m = { |
1.14 Flattening Lists
1, 2], [3, 4], [5, 6]] a = [[ |
1.15 Generator Expressions
2 for x in xrange(10)) g = (x ** |
1.16 Dictionary Comprehensions
2 for x in range(5)} m = {x: x ** |
1.17 Inverting a Dictionary Using a Dictionary Comprehension
'a': 1, 'b': 2, 'c': 3, 'd': 4} m = { |
1.18 Named Tuples (collections.namedtuple)
'Point', ['x', 'y']) Point = collections.namedtuple( |
1.0
p.y |
1.19 Inheriting from Named Tuples
class Point(collections.namedtuple('PointBase', ['x', 'y'])): |
1.20 Sets and Set Operations
1, 2, 3, 3} A = { |
1.21 Multisets and Multiset Operations (collections.Counter)
1, 2, 2]) A = collections.Counter([ |
1.22 Most Common Elements in an Iterable (collections.Counter)
1, 1, 2, 2, 3, 3, 3, 3, 4, 5, 6, 7]) A = collections.Counter([ |
1.23 Double-ended Queue (collections.deque)
Q = collections.deque() |
1.24 Double-ended Queue with Maximum Length (collections.deque)
3) last_three = collections.deque(maxlen= |
1.25 Ordered Dictionaries (collections.OrderedDict)
dict((str(x), x) for x in range(10)) m = |
1.26 Default Dictionaries (collections.defaultdict)
dict() m = |
1.27 Using Default Dictionaries to Represent Simple Trees
import json |
1.28 Mapping Objects to Unique counting Numbers (collections.defaultdict)
import itertools, collections |
1.29 Largest and Smallest Elements (heapq.nlargest and heapq.nsmallest)
0, 100) for __ in xrange(100)] a = [random.randint( |
1.30 Cartesian Products (itertools.product)
for p in itertools.product([1, 2, 3], [4, 5]): |
1.31 Combinations and Combinations with Replacement (itertools.combinations and itertools.combinations_with_replacement)
for c in itertools.combinations([1, 2, 3, 4, 5], 3): |
1.32 Permutations (itertools.permutations)
for p in itertools.permutations([1, 2, 3, 4]): |
1.33 Chaining Iterables (itertools.chain)
1, 2, 3, 4] a = [ |
1.34 Grouping Rows by a given Key (itertools.groupby)
import itertools |
via: http://sahandsaba.com/thirty-python-language-features-and-tricks-you-may-not-know.html
The Why·Liam·Blog by WhyLiam is licensed under a Creative Commons BY-NC-ND 4.0 International License.
由WhyLiam创作并维护的Why·Liam·Blog采用创作共用保留署名-非商业-禁止演绎4.0国际许可证。
本文首发于Why·Liam·Blog (https://blog.naaln.com),版权所有,侵权必究。
本文永久链接:https://blog.naaln.com/2014/03/30-python-language-features-tricks/