python cookbook读书笔记
来源:互联网 发布:sql语句大全实例 编辑:程序博客网 时间:2024/05/16 03:00
本文是阅读Python cookbook第三版的一些笔记。
1、 zip()产生的生成器只能使用一次。
如:
prices_and_names = zip(prices.values(), prices.keys())print(min(prices_and_names)) # OKprint(max(prices_and_names)) # ValueError: max() arg is an empty sequence正确使用:
min_price = min(zip(prices.values(), prices.keys()))# min_price is (10.75, 'FB')max_price = max(zip(prices.values(), prices.keys()))# max_price is (612.78, 'AAPL')
此外,要达成上面的效果,即输出价格单价和名称,通过key=lambda k:price[k]无法实现。
zip完后是tuple进行max or min,默认比较第一个元素,若第一个元素相同,比较后续元素。
2、collections
defaultdict、OrderedDict、deque等
3、heapq 默认最小堆
nsmallest、nlargest 可以通过key对排序函数进行指定
heapify,_heapify_max 进行最小堆化,还能进行最大堆化
4、字典中,键值对的键是唯一的,重复赋值会将之前的覆盖
5、& - 做交集和差集
set(a.items()) & set(b.items())set(a.items()) - set(b.items())
6、对列表去重,并且元素相对顺序不变。
def dedupe(items): seen = set() for item in items: if item not in seen: yield item seen.add(item)>>> a = [1, 5, 2, 1, 9, 1, 5, 10]>>> list(dedupe(a))[1, 5, 2, 9, 10]
以上只能用于传入参数中元素可hashable的情况下。
def dedupe(items, key=None): seen = set() for item in items: val = item if key is None else key(item) if val not in seen: yield item seen.add(val)>>> a = [ {'x':1, 'y':2}, {'x':1, 'y':3}, {'x':1, 'y':2}, {'x':2, 'y':4}]>>> list(dedupe(a, key=lambda d: (d['x'],d['y'])))[{'x': 1, 'y': 2}, {'x': 1, 'y': 3}, {'x': 2, 'y': 4}]>>> list(dedupe(a, key=lambda d: d['x']))[{'x': 1, 'y': 2}, {'x': 2, 'y': 4}]
生成器的作用:
The use of a generator function in this recipe reflects the fact that you might want the function to beextremely general purpose—not necessarily tied directly to list processing. For example, if you wantto read a file, eliminating duplicate lines, you could simply do this:with open(somefile,'r') as f: for line in dedupe(f): ...The specification of a key function mimics similar functionality in built-in functions such assorted() , min() , and max() .
简单的去重,只需set(a)即可。
0 0
- python cookbook读书笔记二
- python cookbook读书笔记
- python数据类型(python cookbook读书笔记一)
- 《Python Testing Cookbook》读书笔记之一:单元测试
- python cookbook 读书笔记1(字符串处理1)
- python cookbook 读书笔记2(字符串处理2)
- python cookbook 读书笔记3(字符串处理3)
- Python Cookbook
- python CookBook
- Python-cookbook
- 【读书笔记】scikit-learn Cookbook
- 处理数字--cookbook读书笔记
- 处理日期--cookbook读书笔记
- 迭代器探索--cookbook读书笔记
- 函数探索--cookbook读书笔记
- Python 小技巧——<Cookbook>读书笔记(1)解压赋值
- Python 小技巧——<Cookbook>读书笔记(2)deque和heapq
- [奥莱理] Apache Sqoop Cookbook-读书笔记
- [Qt C++] QThread的正确用法
- 12eqfsdfds
- oj2011: C语言实验——找中间数
- 安卓使用MediaPlayer播放asset目录下的音乐MP3和raw目录下的音乐MP3
- class="ui-btn"指定居中格式
- python cookbook读书笔记
- CentOS下mysql多实例实现主从复制
- PAT 乙级 1034. 有理数四则运算(20) Java版
- Ubuntu 完全卸载LibreOffice全部组件
- new String("abc")和“abc”的区别
- BZOJ1026: [SCOI2009]windy数
- js 学习笔记
- Android ImageView ScaleType 图解
- 一步一步在Windows中使用MyCat负载均衡 上篇