Python模块之collections

 

更多技术资讯可关注:gzitcast

collections模块
Python3中,在内置数据类型:字典(dict),列表(list),集合(set),元组(tuple),的基础上,collections模块还提供了几个额外封装的数据类型: Counter,deque,defaultdict,namedtuple,OrderDict等
namedtuple
  • 生成能够使用名字来访问元素的元组(tuple)
  • 从名字可以理解,带名字的元组,即我们可以通过name来取值
  • 官方文档解释: 返回一个具有命名字段的元组的新子类
[Python]  纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
In [ 1 ]: # 从collections模块中导入namedtuple                                    
 
In [ 2 ]: from collections import namedtuple                                     
 
In [ 3 ]: # 创建新的元组子类                                                     
 
In [ 4 ]: Point = namedtuple( 'Point' , [ 'name' , 'age' ])                           
 
In [ 5 ]: p = Point( 'zs' , 18 )                                                    
 
In [ 6 ]: # 通过name取值                                                         
 
In [ 7 ]: p.name                                                                 
Out[ 7 ]: 'zs'
 
In [ 8 ]: p.age                                                                  
Out[ 8 ]: 18

deque
  • 双向队列,可以快速的从另外一侧追加和推出对象
  • 使用list存储数据时,按索引访问元素很快,但是插入和删除元素就很慢了,因为列表是线性存储,数量大的时候,插入和删除的效率很低.
  • deque就是为了高效实现插入和删除操作的双向列表,适合用于队列和栈.
[Python]  纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
In [ 1 ]: # 从collections模块中导入deque                                         
 
In [ 2 ]: from collections import deque                                          
 
In [ 3 ]: # 创建双向队列                                                         
 
In [ 4 ]: q = deque([ 'a' , 'b' , 'c' ])                                             
 
In [ 5 ]: # 从最后插入                                                           
 
In [ 6 ]: q.append( 'd' )                                                          
 
In [ 7 ]: # 从头插入                                                             
 
In [ 8 ]: q.appendleft( '0' )                                                      
 
In [ 9 ]: q                                                                      
Out[ 9 ]: deque([ '0' , 'a' , 'b' , 'c' , 'd' ])

Counter
  • Counter类的目的是用来统计容器中值出现的次数;
  • 它是一个无序的容器类型,以字典的键值对形式存储;
  • 其中元素作为key,其计数作为value;
[Python]  纯文本查看 复制代码
?
1
2
3
4
5
6
In [ 1 ]: from collections import Counter                                        
 
In [ 2 ]: data = Counter( 'fsadjkfskdjlakjfdkl' )                                  
 
In [ 3 ]: data                                                                   
Out[ 3 ]: Counter({ 'f' : 3 , 's' : 2 , 'a' : 2 , 'd' : 3 , 'j' : 3 , 'k' : 4 , 'l' : 2 })


OrderedDict
  • 有序字典,顾名思义,Python原本的字典是无序的,在对字典进行迭代时,key的顺序是无法控制的,这时候我们可以利用OrderedDict来实现我们的目标
[Python]  纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
# 从collections模块中盗图OrderedDict
>>> from collections import OrderedDict
# 创建一个无序字典
>>> d = dict ([( 'a' , 1 ),( 'b' , 2 ),( 'c' , 3 )])
>>> d # dict的key是无序的
{ 'a' : 1 , 'c' : 3 , 'b' : 2 }
# 创建一个有序字典
>>> od = OrderedDict([( 'a' , 1 ),( 'b' , 2 ),( 'c' , 3 )])
>>> od  # OrderedDict的key是有序的
OrderedDict([( 'a' , 1 ),( 'b' , 2 ),( 'c' , 3 )])


OrderedDict的key会按照插入的顺序排序,不是key本身排序
[Python]  纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
In [ 1 ]: from collections import OrderedDict                                    
 
In [ 2 ]: od = OrderedDict()                                                     
 
In [ 3 ]: od                                                                     
Out[ 3 ]: OrderedDict()
 
In [ 4 ]: # 插入键值对                                                           
 
In [ 5 ]: od[ 'z' ] = 1                                                            
 
In [ 6 ]: od[ 'y' ] = 2                                                            
 
In [ 7 ]: od[ 'x' ] = 3                                                            
 
In [ 8 ]: od.keys()  # 按照插入的key的顺序返回                                   
Out[ 8 ]: odict_keys([ 'z' , 'y' , 'x' ])
 
In [ 9 ]: od                                                                     
Out[ 9 ]: OrderedDict([( 'z' , 1 ), ( 'y' , 2 ), ( 'x' , 3 )])

defaultdict
  • 带有默认值的字典
  • 当我们使用字典,通过key来获取数据时,如果key不存在,会抛出KeyError异常,如果希望Key不存在时,返回一个默认追,就可以使用defaultdict
[Python]  纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
In [ 1 ]: # 从collections 模块导入defaultdict                                    
 
In [ 2 ]: from collections import defaultdict                                    
 
In [ 3 ]: # 创建带默认值的字典                                                   
 
In [ 4 ]: dd = defaultdict( lambda : None ) # 不存在返回None                        
 
In [ 5 ]: # 为创建的字典添加键值对                                               
 
In [ 6 ]: dd[ 'name' ] = 'qiangzai'                                                
 
In [ 7 ]: # key不存在自动添加返回默认值                                          
 
In [ 8 ]: dd[ 'name' ]                                                             
Out[ 8 ]: 'qiangzai'
 
In [ 9 ]: dd[ 'age' ]                                                              
 
In [ 10 ]: dd                                                                    
Out[ 10 ]: defaultdict( lambda >()>, { 'name' : 'qiangzai' , 'age' : None })
 
In [ 11 ]: dict (dd)                                                              
Out[ 11 ]: { 'name' : 'qiangzai' , 'age' : None }

 

你可能感兴趣的:(Python模块之collections)