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 ...
随机推荐
- HTML5: HTML5 表单元素
ylbtech-HTML5: HTML5 表单元素 1.返回顶部 1. HTML5 表单元素 HTML5 新的表单元素 HTML5 有以下新的表单元素: <datalist> <ke ...
- webstorm 插件安装
1.打勾的表示已经安装 2.没有安装的插件,可以在plugins搜索,在右边搜索结果里点install,然后重启webstorm 3.这里有常用插件 http://blog.csdn.net/xs20 ...
- ZABBIX 忘记登录密码
ZABBIX 忘记登录密码 摘要 有些童鞋会忘记zabbix的登陆密码,今天给大家写一篇找回登陆密码~ ZABBIX 忘记登录密码 zabbix 刚刚在群里吹牛逼,由于账号比较多,脑子容易 ...
- [BJWC2018]上学路线
Description 小B 所在的城市的道路构成了一个方形网格,它的西南角为(0,0),东北角为(N,M). 小B 家住在西南角,学校在东北角.现在有T 个路口进行施工,小B 不能通过这些路口.小B ...
- Socket传输中文乱码
最近在学习Socket的时候,遇到了中文乱码问题,在网上找到了一个说的很透彻的,这里分享一下:http://helloworlda.iteye.com/blog/1270703 现在问题是这样的: 打 ...
- cenos7 下数据库相关操作
1.Linux Centos7下如何确认MySQL服务已经启动 https://www.cnblogs.com/qianzf/p/7082484.html 2.CentOS 7上安装MySQL并配置远 ...
- todolist拆分为逻辑页面和ui页面
我们可以把Todolist 继续拆分 ,拆分为逻辑页面和ui页面 ui 页面 import React, { Component } from 'react';import 'antd/dist/an ...
- leetcood学习笔记-28-KMP*
题目: 第一次提交: class Solution: def strStr(self, haystack: str, needle: str) -> int: if not len(needle ...
- Delphi 类(TApplication)
TApplication类用于开发窗口程序的类.此类封装了一个窗口应用程序,其方法和属性反映了窗口操作系统在建立.执行.维持以及析构该程序等方面的基本原则.Delphi的每个窗口程序都会自动声明一个A ...
- delphi 窗体的位置和高宽度-TForm:Letf、Top、Width、Height、ClientWidth、ClientHeight
delphi 窗体的位置和高宽度-TForm:Letf.Top.Width.Height.ClientWidth.ClientHeight [窗体的高度和宽度]: [客户区的高度和宽度]: [窗体在屏 ...