The Collections Module内建collections集合模块
https://www.bilibili.com/video/av17396749/?p=12
Python函数式编程中的迭代器,生成器详解
课程内容
1.iterators are objects that contain other objects
2.some built-in iterators are such as list,dict,tuple, and set.
3.learned the collections module offers other convenient iterators
4.generators are functions that yield and they are also iterators
5.understood that fenerators allow for lazy evaluation and coroutines,or light-weight threading.
以下是笔记
collections.namedtuple
namedtuple() is a factory function;that is,it generates a class,and not an instance of a class(an object)
two steps:
1.First,use namedtuple() to generate a class
2.then create an instance of this class
from collections import namedtuple
Person=namedtuple('Person',['name','age'])
jay_z=Person(name='Sean Carter',age=47)
print('%s is %s years old'%(jay_z.name,jay_z.age))
print('%s is %s years old'%(jay_z[0],jay_z[1])) 输出 Sean Carter is 47 years old
Sean Carter is 47 years old
collections.OrderedDict
from collections import OrderedDict
d=OrderedDict([
('Lizard','Reptile'),
('Whale','Mammal')
]
) for species,_class in d.items():
print('%s is a %s'%(species,_class)) 输出: Lizard is a Reptile
Whale is a Mammal
collections.defaultdict
from collections import defaultdict
languages={
'Jack':'java',
'Pony':'Ruby',
'Sara':'javascript'
}
d=defaultdict(lambda:'Python')
d.update(languages) 输出:
python
The Collections Module内建collections集合模块的更多相关文章
- 查看Python的版本、内建方法和模块等内容的方法
若想更好地应用Python帮助我们解决日常生活的问题,就必须了解清楚它的内建方法和模块等特性.相信不少同学在安装某个版本的Python后,对于内建方法之类都是一知半解,希望本文能帮助了解Python的 ...
- collections:内建模块,提供额外的集合类
介绍 collections里面包含了很多除了内置类型之外的数据类型,我们使用它们有时可以很方便的完成一系列操作 ChainMap:搜索多个字典 from collections import Cha ...
- Python3 内建模块 datetime/collections/base64/struct
datetime 我们先看如何获取当前日期和时间: >>> from datetime import datetime >>> now = datetime.now ...
- python常用内建模块 collections,bs64,struct,hashlib,itertools,contextlib,xml
# 2 collections 是Python内建的一个集合模块,提供了许多有用的集合类. # 2.1 namedtuple #tuple可以表示不变集合,例如,一个点的二维坐标就可以表示成: p ...
- 四十二 常用内建模块 collections
collections是Python内建的一个集合模块,提供了许多有用的集合类. namedtuple 我们知道tuple可以表示不变集合,例如,一个点的二维坐标就可以表示成: >>> ...
- collections(python常用内建模块)
文章来源:https://www.liaoxuefeng.com/wiki/897692888725344/973805065315456 collections collections是Python ...
- Python内建模块--collections
python内建模块--collections collections是Python内建的一个集合模块,提供了许多有用的集合类. namedtuple 我们知道tuple可以表示不变集合,例如,一个点 ...
- collections集合模块 [namedtuple,deque,*]
collections是Python内建的一个集合模块,提供了许多有用的集合类. namedtuple namedtuple是一个函数, 它用来创建一个自定义的tuple对象,并且规定了 tuple元 ...
- Python自建collections模块
本篇将学习python的另一个内建模块collections,更多内容请参考:Python学习指南 collections是Python内建的一个集合模块,提供了许多有用的集合类. namedtupl ...
随机推荐
- FD_CLOEXEC用法及原因_转
转自:使用FD_CLOEXEC实现close-on-exec,关闭子进程无用文件描述符 我们经常会碰到需要fork子进程的情况,而且子进程很可能会继续exec新的程序.这就不得不提到子进程中无用文件描 ...
- IOS设计模式浅析之抽象工厂模式(Abstract Factory)
概述 在前面两章中,分别介绍了简单工厂模式和工厂方法模式,我们知道简单工厂模式的优点是去除了客户端与具体产品的依赖,缺点是违反了“开放-关闭原则”:工厂方法模式克服了简单工厂模式的缺点,将产品的创建工 ...
- Proving NP-completeness
Proving NP-completeness by generalization. For each of the problems below, prove that it is NP-compl ...
- deque双端队列用法
#include <iostream> #include <cstdio> #include <deque> #include <algorithm> ...
- 009android初级篇之APP中使用系统相机相册等集成应用
android应用中使用相机功能,大致有两种方式实现: 直接调用系统内部的相机程序,显示的也是系统预设的界面(简单,只有简单的拍照功能): 自己去implement一个相机程序(不难,较具备弹性,但相 ...
- Google Careers 程序员必修课
quote from : https://www.google.com/about/careers/students/guide-to-technical-development.html Techn ...
- iOS --提取本地<通讯录>数据 <1>
#pragma mark - 获取手机所有联系人 - (void)obtainPhoneAllContact { //这个变量用于记录授权是否成功,即用户是否允许我们访问通讯录 ; //声明一个通讯簿 ...
- 如何通过PHP判断年份是否是闰年----两种方法
1.定义:闰年是对4取余为0,对100取余不等于0,对400取余等于0的年是闰年. 2.代码: 第一种方法:直接函数判断 $day = date('Y'); if ($day%4==0&&am ...
- spring cloud 订单调用用户
下面实现一个订单调用用户实现例子,使用技术只要是spring,为以后操作负载打基础.(基于昨天别人问我的基础上做了实例供大家参考) 1.用户工程截图 : 2.用户工程启动类 3.用户工程控制类 4. ...
- @Resource 注解
@Resource 注解被用来激活一个命名资源(named resource)的依赖注入,在JavaEE应用程序中,该注解被典型地转换为绑定于JNDI context中的一个对象. Spring确实支 ...