读书笔记
feiyu不瘦
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
python cookbook:1 使用*表达式来解压序列和可迭代对象赋值给多个变量
对于解压赋值,前提条件是'='左右两边的数量必须一致。 如: a = (1, 2,3) x, y, z= a 这种赋值可以用于任何可迭代对象,包括:列表、元祖、字符串、迭代器、生成器等。 当只需要解压一部分数据时,可以通过任意变量名的方式占位。 如: >>> data = [ 'ACME', 50, 91.1, (2012, 12, 21) ] >>&...原创 2019-06-05 15:41:24 · 172 阅读 · 0 评论 -
python cookbook:2 如何保留最后N个元素
from collections import deque def search(lines, pattern, history = 5): previous_lines = deque(maxlen=history) #双端队列 for line in lines: if pattern in line: yield line, pre...原创 2019-06-05 15:58:47 · 281 阅读 · 0 评论 -
python cookbook:3 查找最大或最小的N个元素
import heapq nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2] print(heapq.nlargest(3, nums)) # Prints [42, 37, 23] print(heapq.nsmallest(3, nums)) # Prints [-4, 1, 2] 两个函数同样支持关键字参数。 portfolio = [ ...原创 2019-06-05 16:03:20 · 187 阅读 · 0 评论
分享