Python小技巧和有趣的内置函数
一个python小技巧的集合和一些很有用的Python内置函数,这些函数简直是屌爆了,我认为每个 Pythoner 都应该知道这些函数。
1.1 拆箱
>>> a, b, c = 1, 2, 3
>>> a, b, c
(1, 2, 3)
>>> a, b, c = [1, 2, 3]
>>> a, b, c
(1, 2, 3)
>>> a, b, c = (2 * i + 1 for i in range(3))
>>> a, b, c
(1, 3, 5)
>>> a, (b, c), d = [1, (2, 3), 4]
>>> a
1
>>> b
2
>>> c
3
>>> d
4
1.2 拆箱变量交换
>>> a, b = 1, 2
>>> a, b = b, a
>>> a, b
(2, 1)
1.3 扩展拆箱(只兼容python3)
>>> a, *b, c = [1, 2, 3, 4, 5]
>>> a
1
>>> b
[2, 3, 4]
>>> c
5
1.4 负数索引
>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> a[-1]
10
>>> a[-3]
8
1.5 切割列表
>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> a[2:8]
[2, 3, 4, 5, 6, 7]
1.6 负数索引切割列表
>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> a[-4:-2]
[7, 8]
1.7指定步长切割列表
>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> a[::2]
[0, 2, 4, 6, 8, 10]
>>> a[::3]
[0, 3, 6, 9]
>>> a[2:8:2]
[2, 4, 6]
1.8 负数步长切割列表
>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> a[::-1]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> a[::-2]
[10, 8, 6, 4, 2, 0]
1.9 列表切割赋值
>>> a = [1, 2, 3, 4, 5]
>>> a[2:3] = [0, 0]
>>> a
[1, 2, 0, 0, 4, 5]
>>> a[1:1] = [8, 9]
>>> a
[1, 8, 9, 2, 0, 0, 4, 5]
>>> a[1:-1] = []
>>> a
[1, 5]
1.10 命名列表切割方式
>>> a = [0, 1, 2, 3, 4, 5]
>>> LASTTHREE = slice(-3, None)
>>> LASTTHREE
slice(-3, None, None)
>>> a[LASTTHREE]
[3, 4, 5]
1.11 列表以及迭代器的压缩和解压缩
>>> a = [1, 2, 3]
>>> b = ['a', 'b', 'c']
>>> z = zip(a, b)
>>> z
[(1, 'a'), (2, 'b'), (3, 'c')]
>>> zip(*z)
[(1, 2, 3), ('a', 'b', 'c')]
1.12 列表相邻元素压缩器
>>> a = [1, 2, 3, 4, 5, 6]
>>> zip(*([iter(a)] * 2))
[(1, 2), (3, 4), (5, 6)]
>>> group_adjacent = lambda a, k: zip(*([iter(a)] * k))
>>> group_adjacent(a, 3)
[(1, 2, 3), (4, 5, 6)]
>>> group_adjacent(a, 2)
[(1, 2), (3, 4), (5, 6)]
>>> group_adjacent(a, 1)
[(1,), (2,), (3,), (4,), (5,), (6,)]
>>> zip(a[::2], a[1::2])
[(1, 2), (3, 4), (5, 6)]
>>> zip(a[::3], a[1::3], a[2::3])
[(1, 2, 3), (4, 5, 6)]
>>> group_adjacent = lambda a, k: zip(*(a[i::k] for i in range(k)))
>>> group_adjacent(a, 3)
[(1, 2, 3), (4, 5, 6)]
>>> group_adjacent(a, 2)
[(1, 2), (3, 4), (5, 6)]
>>> group_adjacent(a, 1)
[(1,), (2,), (3,), (4,), (5,), (6,)]
1.13 在列表中用压缩器和迭代器滑动取值窗口
>>> def n_grams(a, n):
... z = [iter(a[i:]) for i in range(n)]
... return zip(*z)
...
>>> a = [1, 2, 3, 4, 5, 6]
>>> n_grams(a, 3)
[(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6)]
>>> n_grams(a, 2)
[(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
>>> n_grams(a, 4)
[(1, 2, 3, 4), (2, 3, 4, 5), (3, 4, 5, 6)]
用压缩器反转字典
>>> m = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> m.items()
[('a', 1), ('c', 3), ('b', 2), ('d', 4)]
>>> zip(m.values(), m.keys())
[(1, 'a'), (3, 'c'), (2, 'b'), (4, 'd')]
>>> mi = dict(zip(m.values(), m.keys()))
>>> mi
{1: 'a', 2: 'b', 3: 'c', 4: 'd'}
1.15 列表展开
>>> a = [[1, 2], [3, 4], [5, 6]]
>>> list(itertools.chain.from_iterable(a))
[1, 2, 3, 4, 5, 6]
>>> sum(a, [])
[1, 2, 3, 4, 5, 6]
>>> [x for l in a for x in l]
[1, 2, 3, 4, 5, 6]
>>> a = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
>>> [x for l1 in a for l2 in l1 for x in l2]
[1, 2, 3, 4, 5, 6, 7, 8]
>>> a = [1, 2, [3, 4], [[5, 6], [7, 8]]]
>>> flatten = lambda x: [y for l in x for y in flatten(l)] if type(x) is list else [x]
>>> flatten(a)
[1, 2, 3, 4, 5, 6, 7, 8]
1.16 生成器表达式
>>> g = (x ** 2 for x in xrange(10))
>>> next(g)
0
>>> next(g)
1
>>> next(g)
4
>>> next(g)
9
>>> sum(x ** 3 for x in xrange(10))
2025
>>> sum(x ** 3 for x in xrange(10) if x % 3 == 1)
408
1.17 字典推导
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
>>> m = {x: 'A' + str(x) for x in range(10)}
>>> m
{0: 'A0', 1: 'A1', 2: 'A2', 3: 'A3', 4: 'A4', 5: 'A5', 6: 'A6', 7: 'A7', 8: 'A8', 9: 'A9'}
1.18 用字典推导反转字典
>>> m = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> m
{'d': 4, 'a': 1, 'b': 2, 'c': 3}
>>> {v: k for k, v in m.items()}
{1: 'a', 2: 'b', 3: 'c', 4: 'd'}
1.19 命名元组
>>> Point = collections.namedtuple('Point', ['x', 'y'])
>>> p = Point(x=1.0, y=2.0)
>>> p
Point(x=1.0, y=2.0)
>>> p.x
1.0
>>> p.y
2.0
函数部分
all(iterable)
如果可迭代的对象(数组,字符串,列表等,下同)中的元素都是true(或者为空)的话返回True
_all = True
for item in iterable:
if not item:
_all = False
break
if _all:
# do stuff
更简便的写法是:
if all(iterable):
# do stuff
any(iterable)
如果可迭代的对象中任何一个元素为true的话返回True,如果可迭代的对象为空则返回False
_any = False
for item in iterable:
if item:
_any = True
break
if _any:
# do stuff
更简便的写法是:
if any(iterable):
# do stuff
cmp(x, y)
比较两个对象 x 和 y , x < y 的时候返回负数, x ==y 的时候返回 0, x > y 的时候返回正数
def compare(x,y):
if x < y:
return -1
elif x == y:
return 0
else:
return 1
你完全可以使用一句 cmp(x, y) 来替代。
dict([arg])
使用 arg 提供的条目生成一个新的字典。
arg 通常是未知的,但是它很方便!比如说,如果我们想把一个含两个元组的列表转换成一个字典,我们可以这么做。
l = [('Knights', 'Ni'), ('Monty', 'Python'), ('SPAM', 'SPAAAM')]
d = dict()
for tuple in l:
d[tuple[0]] = tuple[1]
# {'Knights': 'Ni', 'Monty': 'Python', 'SPAM': 'SPAAAM'}
或者这样:
l = [('Knights', 'Ni'), ('Monty', 'Python'), ('SPAM', 'SPAAAM')]
d = dict(l) # {'Knights': 'Ni', 'Monty': 'Python', 'SPAM': 'SPAAAM'}
enumerate(iterable [,start=0])
我真的是超级喜欢这个!如果你以前写过C语言,那么你可能会这么写:
for i in range(len(list)):
# do stuff with list[i], for example, print it
print i, list[i]
噢,不用那么麻烦!你可以使用enumerate()来提高可读性。
for i, item in enumerate(list):
# so stuff with item, for example print it
print i, item
isinstance(object, classinfo)
如果 object 参数是 classinfo 参数的一个实例或者子类(直接或者间接)的话返回 True
当你想检验一个对象的类型的时候,第一个想到的应该是使用type()函数
if type(obj) == type(dict):
# do stuff
elif type(obj) == type(list):
# do other stuff
...
或者你可以这么写
if isinstance(obj, dict):
# do stuff
elif isinstance(obj, list):
# do other stuff
...
pow(x, y [,z])
返回 x 的 y 次幂(如果 z 存在的话则以 z 为模)。
如果你想计算 x 的 y 次方,以 z 为模,那么你可以这么写:
mod = (x ** y) % z
但是当 x=1234567, y=4567676, z=56 的时候我的电脑足足跑了 64 秒!
不要用 ** 和 % 了,使用 pow(x, y, z) 吧!这个例子可以写成 pow(1234567, 4567676, 56) ,只用了 0.034 秒就出了结果!
zip([iterable, ])
这个函数返回一个含元组的列表,具体请看例子:
l1 = ('You gotta', 'the')
l2 = ('love', 'built-in')
out = []
if len(l1) == len(l2):
for i in range(len(l1)):
out.append((l1[i], l2[i]))
# out = [('You gotta', 'love'), ('the', 'built-in)]
或者这么写:
l1 = ['You gotta', 'the']
l2 = ['love', 'built-in']
out = zip(l1, l2) # [('You gotta', 'love'), ('the', 'built-in)]
如果你想得到倒序的话加上 * 操作符就可以了:
print zip(*out)
# [('You gotta', 'the'), ('love', 'built-in')]
结论
Python 内置函数很方便,它们很快并且经过了优化,所以它们可能效率更高。
我真心认为每个 Python 开发者都应该好好看看内置函数的文档(引言部分)。
忘了说了,在 itertools 模块中有很多很不错的函数。