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集合模块的更多相关文章

  1. 查看Python的版本、内建方法和模块等内容的方法

    若想更好地应用Python帮助我们解决日常生活的问题,就必须了解清楚它的内建方法和模块等特性.相信不少同学在安装某个版本的Python后,对于内建方法之类都是一知半解,希望本文能帮助了解Python的 ...

  2. collections:内建模块,提供额外的集合类

    介绍 collections里面包含了很多除了内置类型之外的数据类型,我们使用它们有时可以很方便的完成一系列操作 ChainMap:搜索多个字典 from collections import Cha ...

  3. Python3 内建模块 datetime/collections/base64/struct

    datetime 我们先看如何获取当前日期和时间: >>> from datetime import datetime >>> now = datetime.now ...

  4. python常用内建模块 collections,bs64,struct,hashlib,itertools,contextlib,xml

    #  2  collections 是Python内建的一个集合模块,提供了许多有用的集合类. # 2.1 namedtuple #tuple可以表示不变集合,例如,一个点的二维坐标就可以表示成: p ...

  5. 四十二 常用内建模块 collections

    collections是Python内建的一个集合模块,提供了许多有用的集合类. namedtuple 我们知道tuple可以表示不变集合,例如,一个点的二维坐标就可以表示成: >>> ...

  6. collections(python常用内建模块)

    文章来源:https://www.liaoxuefeng.com/wiki/897692888725344/973805065315456 collections collections是Python ...

  7. Python内建模块--collections

    python内建模块--collections collections是Python内建的一个集合模块,提供了许多有用的集合类. namedtuple 我们知道tuple可以表示不变集合,例如,一个点 ...

  8. collections集合模块 [namedtuple,deque,*]

    collections是Python内建的一个集合模块,提供了许多有用的集合类. namedtuple namedtuple是一个函数, 它用来创建一个自定义的tuple对象,并且规定了 tuple元 ...

  9. Python自建collections模块

    本篇将学习python的另一个内建模块collections,更多内容请参考:Python学习指南 collections是Python内建的一个集合模块,提供了许多有用的集合类. namedtupl ...

随机推荐

  1. input子系统分析(转)

    转自:http://www.linuxidc.com/Linux/2011-09/43187.htm 作者:作者:YAOZHENGUO2006 Input子系统处理输入事务,任何输入设备的驱动程序都可 ...

  2. python 使用正则表达式的爬虫

    下面我们一起尝试一下爬取内涵段子网站: http://www.neihan8.com/article/list_5_1.html 打开之后,不难看到里面一个一个灰常有内涵的段子,当你进行翻页的时候,注 ...

  3. Python 爬虫实战2 百度贴吧帖子

    爬取百度贴吧的帖子.与上一篇不同的是,这次我们需要用到文件的相关操作. 本篇目标 对百度贴吧的任意帖子进行抓取 指定是否只抓取楼主发帖内容 将抓取到的内容分析并保存到文件 1.URL格式的确定 首先, ...

  4. Jqurey实现相似EasyUI的页面布局

    截图例如以下:(可通过移动中间蓝色的条.来改变左右两边div的宽度) watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFuZ21pbmd4aW5nOTgw ...

  5. python学习【第八篇】python模块

    模块与包 模块的概念 在python中一个.py文件就是一个模块. 使用模块可以提高代码的可维护性. 模块分为三种: python标准库 第三方模块 自定义模块 模块的导入方法 1.import语句 ...

  6. P2424 约数和

    题目背景 Smart最近沉迷于对约数的研究中. 题目描述 对于一个数X,函数f(X)表示X所有约数的和.例如:f(6)=1+2+3+6=12.对于一个X,Smart可以很快的算出f(X).现在的问题是 ...

  7. FZU 1063 三维扫描(三维连通块)

    Accept: 415    Submit: 1291 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description 工业 ...

  8. 数据库几种连接方式的(左右union all)

    A表 id  name 1 bai 2 zhang 3 li B表 a_id score 1 80 2 70 1 30 左连接: select A.* ,B.* from A a left outer ...

  9. git github usage

    以gerrit-trigger-plugin为例,下面的链接都是从相应页面上直接拷贝的. 法一:不用github的账号,打开这个库在github上的主页,运行下面命令即可 read only 运行命令 ...

  10. 判断SQL数据库中函数、存储过程等是否存在的方法

    下面为您介绍sql下用了判断各种资源是否存在的代码,需要的朋友可以参考下,希望对您学习sql的函数及数据库能够有所帮助.库是否存在if exists(select * from master..sys ...