本文首发于微信公众号:NewBeeNLP
写在前面
我又来更新啦~今天一起回顾下Python Cookbook,全书是以问答对
的形式展开,这是我很久之前看的笔记。Cookbook不算是入门书,更像是一本工具书,既然有基础了那就没必要一个个点去看,建议是需要用到那部分就把那块的知识点技巧翻一遍。下面大噶自己查漏补缺吧!
Chap1 数据结构与算法
从任意长度的可迭代对象中分解元素
*表达式
可以用来将一个含有N个元素的数据结构类型分解成所需的几部分。
例如grades保存了100个成绩数据而我们只关心首末两个成绩,就可以把中间的所有成绩保存到一个列表里面,如下:
first, *middle, last = grades
保存最后N个元素
from collection import deque
q = deque()
q.append(1)
q.append(2)
q.append(3)
q.appendleft(4)
q.pop()
q.popleft()
找到最大或最小的N个元素
import heapq
nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]
print(heapq.nlargest(3, nums))
out: [42, 37, 23]
print(heapq.nsmallest(3,nums))
out: [-4, 1, 2]
In [1]: portfolio = [
...: {'name': 'IBM', 'shares': 100, 'price': 91.1},
...: {'name': 'AAPL', 'shares': 50, 'price': 543.22},
...: {'name': 'FB', 'shares': 200, 'price': 21.09},
...: {'name': 'HPQ', 'shares': 35, 'price': 31.75},
...: {'name': 'YHOO', 'shares': 45, 'price': 16.35},
...: {'name': 'ACME', 'shares': 75, 'price': 115.65}
...: ]
cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['price'])
cheap
out:
[{'name': 'YHOO', 'price': 16.35, 'shares': 45},
{'name': 'FB', 'price': 21.09, 'shares': 200},
{'name': 'HPQ', 'price': 31.75, 'shares': 35}]
让字典保持有序
字典的计算问题
找出序列中出现次数最多的元素
from collections import Counter
words = [一系列单词组成的列表]
word_counts = Counter(words)
top_3 = word_counts.most_common(3)
通过公共键对字典列表排序
from operator import itemgetter
In [26]: rows = [
...: {'fname': 'Brian', 'lname': 'Jones', 'uid':1003},
...: {'fname': 'David', 'lname': 'Beazley', 'uid':1002},
...: {'fname': 'John', 'lname': 'Cleese', 'uid':1001},
...: {'fname': 'Big', 'lname': 'Jones', 'uid':1004}
...: ]
itemgetter('fname')
Out[31]:
rows_by_frame = sorted(rows, key=itemgetter('fname'))
Out[30]:
[{'fname': 'Big', 'lname': 'Jones', 'uid': 1004},
{'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},
{'fname': 'David', 'lname': 'Beazley', 'uid': 1002},
{'fname': 'John', 'lname': 'Cleese', 'uid': 1001}]
rows_by_frame = sorted(rows, key=itemgetter('fname','lname'))
Chap3 数字、日期和时间
对数值进行取整
>>> round(1.23, 1)
1.2
>>> round(1.27, 1)
1.3
>>> round(162773, -1)
162770
对数值做格式化输出
>>>x = 123.456
>>>format(x, '0.2f')
123.46
二进制、八进制和十六进制转换
随机选择
>>>import random
>>>values = [1,2,3,4,5,6]
>>>random.choice(values)
4
>>>random.choice(values)
2
>>>random.shuffle(values)
>>>values
[2,4,3,1,6,5]
>>>random.sample(values, 2)
[6, 2]
时间换算
from datetime import timedelta
In [33]: a = timedelta(days=2, hours=6)
In [34]: b = timedelta(hours=4.5)
In [35]: c = a + b
In [36]: c.days
Out[36]: 2
In [37]: c.seconds
Out[37]: 37800
In [38]: c.seconds/3600
Out[38]: 10.5
In [39]: c.total_seconds() / 3600
Out[39]: 58.5
Chap4 迭代器和生成器
手动访问迭代器中的元素
with open('/etc/passwd') as f:
try:
while True:
line = next(f)
print(line, end='')
except StopIteration:
pass
委托迭代
class Node:
def __init__(self, value):
Self._value = vaule
self._children = []
def __repr__(self):
return 'Node({!r})'.format(self._value)
def __iter__(self):
return iter(self._children)
在这个例子中,iter()方法将迭代请求转发给对象内部持有的_children属性上。
用生成器创建新的迭代模式
def frange(start, stop, increment):
x = start
while x < stop:
yield x
x += increment
对迭代器做切片操作
In [3]: def count(n):
...: while True:
...: yield n
...: n += 1
...:
In [5]: c = count(0)
In [6]: c
Out[6]:
----> 1 c[0]
TypeError: 'generator' object has no attribute '__getitem__'
import itertools
In [10]: for x in itertools.islice(c, 10, 20):
...: print(x)
10
11
12
13
14
15
16
17
18
19
跳过可迭代对象中的前一部分元素
例如,我们需要读取一个文件,文件的开头有一系列注释行并不是我们想要的
from itertools import dropwhile
with open('/etc/passwd') as f:
for line in dropwhile(lambda line: line,startwith('#'), f):
print(line, end='')
迭代所有可能的组合
In [11]: from itertools import permutations
In [12]: items = ['a', 'b', 'c']
In [13]: for p in permutations(items):
...: print(p)
...:
('a', 'b', 'c')
('a', 'c', 'b')
('b', 'a', 'c')
('b', 'c', 'a')
('c', 'a', 'b')
('c', 'b', 'a')
#如果想得到较短的所有全排列,可以指定长度
In [14]: for p in permutations(items, 2):
...: print(p)
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'c')
('c', 'a')
('c', 'b')
同时迭代多个序列
>>>xvalues = [1,5,4,2,10,7]
>>> yvalues = [101,78,37,15,62,99]
>>> for x, y in zip(xvalues, yvalues):
... print(x, y)
...
1 101
5 78
4 37
2 15
10 62
7 99
在不同的容器中进行迭代
from itertools import chain
In [18]: a = [1, 2, 3, 4]
In [19]: b = ['x', 'y', 'z']
In [20]: for x in chain(a, b):
...: print (x)
...:
1
2
3
4
x
y
z
合并多个有序序列,再对整个有序序列进行迭代
>>>import heapq
>>>a = [1,4,7,10]
>>>b = [2,5,6,11]
>>>for c in heapq.merge(a,b):
... print(c)
...
1
2
4
5
6
7
10
11
Chap 5 文件和IO
将输出重定向到文件中
with open('somefile.txt', 'rt') as f:
print('Hello World!', file=f)
以不同的分隔符或行结尾符完成打印
>>>print('GKY',1995,5,18, sep='-',end='!!\n')
GKY-1995-5-18!!
在字符串上执行IO操作
读写压缩的数据文件
import gzip
with open('somefile.gz', 'rt') as f:
text = f.read()
import bz2
with open('somefile.bz2', 'rt') as f:
text = f.read()
import gzip
with open('somefile.gz', 'wt') as f:
f.write(text)
import bz2
with open('somefile.bz', 'wt') as f:
f.write(text)
将二进制数据读到可变缓冲区中
import os.path
def read_into_buffer(filename):
buf = bytearray(os.path.getsize(filename))
with open(filename, 'rb') as f:
f.readinto(buf)
return buf
with open('sample.bin', 'wb') as f:
f.write(b'hello world')
buf = read_into_buffer('sample.bin')
In [16]: buf
Out[16]: bytearray(b'hello world')
序列化Python对象
import pickle
data = ... #some python object
f = open('somefile', 'wb')
pickle.dump(data,f)
import pickle
data = ... #some python object
f = open('somefile', 'wb')
pickle.dumps(data,f)
Chap 6 数据编码与处理
读写JSON数据
解析简单的XML文档
from urllib.request import urlopen
from xml.etree.ElementTree import parse
u = urlopen('http://planet.python.org/rss20.xml')
doc = parse(u)
In [24]: for item in doc.iterfind('channel/item'):
....: title = item.findtext('title')
....: date = item.findtext('pubDate')
....: link = item.findtext('link')
....: print (title)
....: print(date)
....: print(link)
....: print()
....:
以上~