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

 1 from collections import namedtuple
 2 Person=namedtuple('Person',['name','age'])
 3 jay_z=Person(name='Sean Carter',age=47)
 4 print('%s is %s years old'%(jay_z.name,jay_z.age))
 5 print('%s is %s years old'%(jay_z[0],jay_z[1]))
 6 
 7 
 8 
 9 输出
10 
11 Sean Carter is 47 years old
12 Sean Carter is 47 years old

 

collections.OrderedDict

 1 from collections import OrderedDict
 2 d=OrderedDict([
 3 ('Lizard','Reptile'),
 4 ('Whale','Mammal')
 5 ]
 6 )
 7 
 8 for species,_class in d.items():
 9 print('%s is a %s'%(species,_class))
10 
11 
12 
13 输出:
14 
15 Lizard is a Reptile
16 Whale is a Mammal

 

collections.defaultdict

 1 from collections import defaultdict
 2 languages={
 3 'Jack':'java',
 4 'Pony':'Ruby',
 5 'Sara':'javascript'
 6 }
 7 d=defaultdict(lambda:'Python')
 8 d.update(languages)
 9 
10 输出:
11 python

 

转载于:https://www.cnblogs.com/xiongxueqi/p/8973930.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值