Python CookBook(self report)
Python CookBook
中文版:https://python3-cookbook.readthedocs.io/zh_CN/latest/copyright.html
英文版:https://d.cxcore.net/Python/Python_Cookbook_3rd_Edition.pdf
Data Structures and Algorithms
start expressions
- items = [1, 10, 7, 4, 5, 9]
- def sum(items):
- ... head, *tail = items
- ... return head + sum(tail) if tail else head
- ...
- >>> head, *tail = items
- >>> head
- 1
- >>> tail
- [10, 7, 4, 5, 9]
- >>> sum(items)
- 36
Deque
Using deque(maxlen=N) creates a fixed-sized queue. When new items are added and the queue is full, the oldest item is automatically removed.
- from collections import deque
- >>> q = deque(maxlen=3)
- >>> q.append(1)
- >>> q.append(2)
- >>> q.append(3)
- >>> q
- deque([1, 2, 3], maxlen=3)
- >>> q.append(4)
- >>> q
- deque([1, 2, 3])
>>> q.appendleft(4)
>>> q deque([4, 1, 2, 3])
>>> q.pop() 3
>>> q deque([4, 1, 2])
>>> q.popleft() 4
★ Adding or popping items from either end of a queue has O(1) complexity. This is unlike a list where inserting or removing items from the front of the list is O(N).
Heapq
- The heapq module has two functions—nlargest() and nsmallest()—that do exactly what you want. For example:
- 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]
- Both functions also accept a key parameter that allows them to be used with more
- complicated data structures. For example:
- 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'])
- expensive = heapq.nlargest(3, portfolio, key=lambda s: s['price'])
- 声明:如果涉及侵权请联系本人‘删除’,谢谢~
Python CookBook(self report)的更多相关文章
- python cookbook学习1
python cookbook学习笔记 第一章 文本(1) 1.1每次处理一个字符(即每次处理一个字符的方式处理字符串) print list('theString') #方法一,转列表 结果:['t ...
- 《Python cookbook》 “定义一个属性可由用户修改的装饰器” 笔记
看<Python cookbook>的时候,第9.5部分,"定义一个属性可由用户修改的装饰器",有个装饰器理解起来花了一些时间,做个笔记免得二刷这本书的时候忘了 完整代 ...
- python书籍推荐:Python Cookbook第三版中文
所属网站分类: 资源下载 > python电子书 作者:熊猫烧香 链接:http://www.pythonheidong.com/blog/article/44/ 来源:python黑洞网 内容 ...
- python cookbook 小结
最近一直在看python cookbook.这本书主要讲的是python 语言的一些编程素材.正如它的名字一样,烹饪书.就好像再讲如何处理食材(各种类型的数据),然后再煮菜(算法).打个比方,煮菜随便 ...
- 实操一下<python cookbook>第三版1
这几天没写代码, 练一下代码. 找的书是<python cookbook>第三版的电子书. *这个操作符,运用得好,确实少很多代码,且清晰易懂. p = (4, 5) x, y = p p ...
- Python Cookbook 笔记--12章并发编程
<Python Cookbook(第3版)中文版> 1.队列queue的有些方法是线程不安全的,在多线程中最好别用 2.需要限制一段代码的并发访问量时,用信号量.不要把信号量当做普通的锁来 ...
- 【python cookbook】找出序列中出现次数最多的元素
问题 <Python Cookbook>中有这么一个问题,给定一个序列,找出该序列出现次数最多的元素.例如: words = [ 'look', 'into', 'my', 'eyes', ...
- Python Cookbook(第3版) 中文版 pdf完整版|网盘下载内附提取码
Python Cookbook(第3版)中文版介绍了Python应用在各个领域中的一些使用技巧和方法,其主题涵盖了数据结构和算法,字符串和文本,数字.日期和时间,迭代器和生成器,文件和I/O,数据编码 ...
- 【python cookbook】【数据结构与算法】19.同时对数据做转换和换算
问题:我们需要调用一个换算函数(例如sum().min().max()),但是首先需对数据做转换或者筛选处理 解决方案:非常优雅的方法---在函数参数中使用生成器表达式 例如: # 计算平方和 num ...
随机推荐
- 76、python学习第二篇
生成随机数的测试数据 ''' Created on 2017年4月8日 @author: weizhen #to create data for testing ''' import random f ...
- 多条件异步搜索+分页(PHP、 AJAX、ThinkPHP)
项目中遇到的多条件异步查询及数据分页问题,做了数次尝试,最终虽目的达到,略有繁琐,希望能有更好的处理方式 基于 tp框架 1.html页面代码 <div class="h_cityNa ...
- css点击高亮
.btn-default:active:focus, .btn-default:active:hover { color: #333; background-color:lightseagreen; ...
- Windows7下移植Qt4.8.4项目到QT5.2上时遇到的一些问题
最近在Windows7下将Qt4.8.4+MSVC2008的项目移植到QT5.2下时,遇到了一些小问题: 问题一:错误:C1083: 无法打开包括文件:"QApplication&q ...
- GNU Linux 64汇编学习
函数调用传参: 第一个参数:rdi, 第二个参数:rsi 函数调用栈结构: 返回值 第一个参数 第二个参数 +----------+ rsp-24 | a | +----------+ rsp-16 ...
- python操作xls表
1.python读取excel中单元格内容为日期的方式 python读取excel中单元格的内容返回的有5种类型,即上面例子中的ctype: ? 1 ctype : 0 empty,1 string, ...
- java入门之:Hello World
import java.util.Scanner; public class HelloWorld{ public static void main(String[] args){ //向终端打印he ...
- collections库的namedtuple+pytest的使用
from collections import namedtupleTask=namedtuple('Task',['summary','owner','done','id'])Task.__new_ ...
- 5、springcloud整合mybatis注解方式
1.上一篇学习了服务提供者provider,但是并不是单单就学习了服务提供者.中间还穿插使用了Hikari数据源和spring cloud整合mybatis.但是上篇使用mybatis时还是沿用了老的 ...
- 使用JDBC获取SQL自动增长的ID
在项目开发中,遇到一个问题,先添加一条记录然后想立刻获取这条记录的ID值,ID由SQLServer自动增长的,如果先插入再查询的话,需要另外执行一条查询ID的SQL语句,因此有了下面的方法: 1.使用 ...