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. file、inode在应用层和驱动层之间的联系_转

    转自:http://blog.csdn.net/dreaming_my_dreams/article/details/8272586 应用层和驱动的衔接,一直是一个老大难问题,若弄不清楚,总觉得驱动写 ...

  2. sed & awk & grep 专题( 鸟哥 )

    grep, sed 与 awk 相当有用 ! gerp 查找, sed 编辑, awk 根据内容分析并处理. awk(关键字:分析&处理) 一行一行的分析处理 awk '条件类型1{动作1}条 ...

  3. 图像增强之DDE---基于红外图像的数字图像细节增强DDE

    (1)DDE应用背景 (2)DDE算法简介 (3)DDE 实现 (4)DDE 总结和不足 ----------author:pkf -----------------time:2-9 -------- ...

  4. load data导入数据之csv的用法

    今天总结一下:csv格式文件导入的方法. 1.准备数据表:CSV

  5. JavaScript核心(晋级高手必读篇)

    本文是对“ECMA-262-3 in detail”系列学习内容的概述与总结.如果你对ES3系列文章感兴趣,本文每一节内容均包含相应ES3系列章节的链接,以供阅读与获取更深入的解释. 本文预期读者:有 ...

  6. 夏日炎炎 python写个天气预报

    东南地区连续突破历史,江浙沪除了包邮之外的另一福利-桑拿也已到手.这样的日子里是应该每日关注天气主义降暑避免出现热疾病,python包含比 较多的网络应用类这样就方便了一些网络应用的操作,之外还有些可 ...

  7. day22模块和包

       一.模块 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的代码(. ...

  8. 不通过AppStore,在iOS设备上直接安装应用程序的原理

    本文转载至  http://mobile.51cto.com/hot-439095.htm 通过itms-services协议,可以通过safari浏览器直接在iOS设备上安装应用程序.利用这种方式, ...

  9. Python HTMLTestRunner报告及BeautifulReport报告

    import unittest import HTMLTestRunner class Testfunc(unittest.TestCase): def testa(self): "&quo ...

  10. 人物FSM

    人物有限状态机 之前看这个状态机没看懂,今天又翻出来,看的略懂 FSM在游戏中应用的地方还是挺多的 怪物AI,玩家行为管理 条件(包含若干事件) 条件(包含若干事件) 状态1<--------- ...