Python数据结构——collections

2024-06-02 13:58

本文主要是介绍Python数据结构——collections,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Python包括很多标准编程数据结构,如 list , tuple , dict , set ,这些属于内置类型
collections模块包含多种数据结构的实现,扩展了其他模块中相应的结构。
Counter是一个容器,可以跟踪相同的值增加了多少次。这个类可以用来实现其他语言常用包或多集合数据结构来实现的算法。
Deque是一个双端队列,允许从任意一端增加或删除元素。
defaultdict是一个字典,如果找不到某个键,会相应一个默认值。
OrderedDict会记住增加元素的序列。
nametuple扩展了一般的 tuple ,除了为每个成员元素提供一个数值索引外还提供了一个属性名。

1.Counter
Counter作为一个容器,可以跟踪相同的值增加了多少次。这个类可以用来实现其他语言常用包或多集合数据结构来实现的算法。
初始化
Counter支持 3 种形式的初始化。调用Counter的构造函数时可以提供一个元素序列或者一个包含键和计数的字典,还可以使用关键字参数将字符串名映射到计数。
import  collections
print   collections.Counter(['a''b''c''a''b''b'])
print   collections.Counter({'a':2'b':3'c':1})
print   collections.Counter(a=2, b=3, c=1)
print   collections.Counter('aabbbc')
这四种形式的初始化结构都是一样的。
>>>  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =  RESTART  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
>>> 
Counter({ 'b' 3 'a' 2 'c' 1 })

如果不提供任何参数,可以构造一个空的Counter,然后通过update()方法填充。
import  collections
=  collections.Counter()
print  'Initial  :' , c
c.update( 'abcdcaa' )
print  'Sequencel:' , c
c.update({ 'a' : 1 'd' : 6 })
print  'Dict     :' , c
计数值将根据新数据增加,替换数据不会改变计数。
>>>  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =  RESTART  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
>>> 
Initial  : Counter()
Sequencel: Counter({ 'a' 3 'c' 2 'b' 1 'd' 1 })
Dict      : Counter({ 'd' 7 'a' 4 'c' 2 'b' 1 })
访问计数
一旦填充了Counter,可以使用字典API获取它的值。
import  collections
=  collections.Counter( 'abcdccca' )
for  letter  in  'abcde' :
     print  '%s : %d'  %  (letter, c[letter])
对于未知元素,Counter不会产生KerError,如果没有找到某个值,其计数为0
>>>  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =  RESTART  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
>>> 
a :  2
b :  1
c :  4
d :  1
elements()方法返回一个迭代器,将生产Counter知道的所有元素
import  collections
=  collections.Counter( 'abcdccca' )
c[ 'e' =  0
print  c
print  list (c.elements())
不能保证元素顺序不变,另外计数小于或等于 0 的元素不包含在内。
>>>  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =  RESTART  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
>>> 
Counter({ 'c' 4 'a' 2 'b' 1 'd' 1 'e' 0 })
[ 'a' 'a' 'c' 'c' 'c' 'c' 'b' 'd' ]
使用most_common()可以生成一个序列,其中包含n个最常遇到的输入值及其相应计数。
import  collections
=  collections.Counter()
with  open (r 'd:\check_traffic.sh' 'rt' ) as f:
           for  line  in  f:
               c.update(line.rstrip().lower())
print  'Most common:'
for  letter, count  in  c.most_common( 5 ):
           print  '%s: %6d'  %  (letter, count)
统计系统所有单词中出现的字母,生成一个频率分布,然后打印 5 个最常见的字母。
>>>  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =  RESTART  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
>>> 
Most common:
  :    6535
e:    3435
     :    3202
t:    3141
i:    3100
算术操作
Counter实例支持算术和集合操作来完成结果的聚集。
import  collections
c1  =  collections.Counter([ 'a' 'a' 'c' 'b'  , 'a' ])
c2  =  collections.Counter( 'alphabet' )
print  'c1:' , c1
print  'c2:' , c2
print  '\nCombined counts:'
print  c1  +  c2
print  '\nSubtraction:'
print  c1  -  c2
print  '\nIntersection:'
print  c1 & c2
print  '\nUnion:'
print  c1 | c2
每次通过操作生成一个新的Counter时,计数为 0 或者负的元素都会被删除。
>>>  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =  RESTART  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
>>> 
c1: Counter({ 'a' 3 'c' 1 'b' 1 })
c2: Counter({ 'a' 2 'b' 1 'e' 1 'h' 1 'l' 1 'p' 1 't' 1 })
Combined counts:
Counter({ 'a' 5 'b' 2 'c' 1 'e' 1 'h' 1 'l' 1 'p' 1 't' 1 })
Subtraction:
Counter({ 'a' 1 'c' 1 })
Intersection:
Counter({ 'a' 2 'b' 1 })
Union:
Counter({ 'a' 3 'c' 1 'b' 1 'e' 1 'h' 1 'l' 1 'p' 1 't' 1 })

2  defaultdict
标准字典包括一个方法setdefault()来获取一个值 ,如果值不存在则建立一个默认值。defaultdict初始化容器是会让调用者提前指定默认值。
import  collections
def  default_factory():
     return  'default value'
= collections.defaultdict(default_factory, foo = 'bar' ) 或者 d = collections.defaultdict(lambda :'333',{'foo' = 'bar'})
print  'd:' , d
print  'foo =>' , d[ 'foo' ]
print  'var =>' , d[ 'bar' ]
只要所有键都有相同的默认值,就可以使用这个方法。
>>>  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =  RESTART  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
>>> 
d: defaultdict(<function default_factory at  0x0201FAB0 >, { 'foo' 'bar' })
foo  = > bar
var  = > default value

3   deque
deque(两端队列)支持从任意一端增加和删除元素。常用的两种结果,即栈和队列,就是两端队列的退化形式,其输入和输出限制在一端。
import  collections
=  collections.deque( 'abcdefg' )
print  'Deque:' , d
print  'Length:' len (d)
print  'Left end' , d[ 0 ]
print  'Right end' , d[ - 1 ]
d.remove( 'c' )
print  'remove(c):' , d
deque是一种序列容器,支持 list 操作,可以通过匹配标识从序列中间删除元素。
>>>  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =  RESTART  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
>>> 
Deque: deque([ 'a' 'b' 'c' 'd' 'e' 'f' 'g' ])
Length:  7
Left end a
Right end g
remove(c): deque([ 'a' 'b' 'd' 'e' 'f' 'g' ])
填充
deque可以从任意一端填充,在python实现称为“左端”和“右端”。
import  collections
d1  =  collections.deque()
d1.extend( 'abcdefg' )
print  'extend:' , d1
d1.append( 'h' )
print  'append:' , d1
d2  =  collections.deque()
d2.extendleft( xrange ( 6 ))
print  'extendleft' , d2
d2.appendleft( 6 )
print  'appendleft' , d2
extendleft()迭代处理其输入,对每个元素完成与appendleft()相同的处理。
>>>  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =  RESTART  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
>>> 
extend: deque([ 'a' 'b' 'c' 'd' 'e' 'f' 'g' ])
append: deque([ 'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' ])
extendleft deque([ 5 4 3 2 1 0 ])
appendleft deque([ 6 5 4 3 2 1 0 ])
利用
可以从两端利用deque元素,取决于应用的算法。
import  collections
print  "From the right:"
=  collections.deque( 'abcdefg' )
while  True :
     try :
         print  d.pop(),
     except  IndexError:
         break
print
print  "\nFrom the left:"
=  collections.deque( xrange ( 6 ))
while  True :
     try :
         print  d.popleft(),
     except  IndexError:
         break
print
使用pop()可以从deque右端删除一个元素,使用popleft()可以从deque左端删除一个元素。
>>>  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =  RESTART  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
>>> 
From the right:
g f e d c b a
From the left:
0  1  2  3  4  5
由于双端队列是线程安全的,可以在不同的线程中同时从两端利用队列的内容。
import  collections
import  threading
import  time
candle  =  collections.deque( xrange ( 5 ))
def  burn(direction, nextSource):
     while  True :
         try :
             next  =  nextSource()
         except  IndexError:
             break
         else :
             print  '%8s: %s'  %  (direction,  next )
             time.sleep( 0.1 )
     print  '%8s done'  %  direction
     return
left  =  threading.Thread(target = burn, args = ( 'Left' , candle.popleft))
right  =  threading.Thread(target = burn, args = ( 'Right' , candle.pop))
left.start()
right.start()
left.join()
right.join()
线程交替处理两端,删除元素,知道这个deque为空。
>>>  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =  RESTART  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
>>> 
     Left:  0    Right:  4
    Right:  3     Left:  1
    Right:  2     Left done
    Right done
旋转
deque另外一个作用可以按照任意一个方向旋转,而跳过一些元素。
import  collections
=  collections.deque( xrange ( 10 ))
print  'Normal:' , d
d =  collections.deque( xrange ( 10 ))
d.rotate( 2 )
print  'Right roration:' , d
=  collections.deque( xrange ( 10 ))
d.rotate( - 2 )
print  'Left roration:' , d
>>>  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =  RESTART  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
>>> 
Normal: deque([ 0 1 2 3 4 5 6 7 8 9 ])
Right roration: deque([ 8 9 0 1 2 3 4 5 6 7 ])
Left roration: deque([ 2 3 4 5 6 7 8 9 0 1 ])

4  namedtuple
标准 tuple 使用数值索引来访问其成员。
nametuple实例与常规元祖在内存使用方面同样高效,因为它们没有各实例的字典。各种nametuple都是由自己的类表示,使用nametuple()工厂函数来创建。参数就是一个新类名和一个包含元素名的字符串。
import  collections
Person  =  collections.namedtuple( 'Persion' 'name age gender' )
print  'Type of Person:' type (Person)
bob  =  Person(name = 'Bob' , age = 30 , gender = 'male' )
print  '\nRepresentation:' , bob
jane  =  Person(name = 'Jane' , age = 28 , gender = 'female' )
print  '\nField by name:' , jane.name
print  '\nField by index:'
for  in  [bob, jane]:
     print  '%s is a %d year old %s'  % p


5  OrderedDict
OrderedDict是一个字典子类,可以记住其内容增加的顺序。
import  collections
print  'Regular dictionary:'
=  {}
d[ 'a' =  'A'
d[ 'b' =  'B'
d[ 'c' =  'C'
for  k, v  in  d.items():
     print  k, v
print  '\nOrderDict:'
=  collections.OrderedDict()
d[ 'a' =  'A'
d[ 'b' =  'B'
d[ 'c' =  'C'
for  k, v  in  d.items():
     print  k, v
常规 dict 并不跟踪插入顺序,迭代处理会根据键在散列表中存储的顺序来生成值。在OrderDict中则相反,它会记住元素插入的顺序,并在创建迭代器时使用这个顺序。
>>>  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =  RESTART  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
>>> 
Regular dictionary:
a A
c C
b B
OrderDict:
a A
b B
c C
常规 dict 在检查相等性是会查看其内容,OrderDict中还会考虑元素增加的顺序。


这篇关于Python数据结构——collections的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1024161

相关文章

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

基于Python打造一个智能单词管理神器

《基于Python打造一个智能单词管理神器》这篇文章主要为大家详细介绍了如何使用Python打造一个智能单词管理神器,从查询到导出的一站式解决,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 项目概述:为什么需要这个工具2. 环境搭建与快速入门2.1 环境要求2.2 首次运行配置3. 核心功能使用指

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财

Python中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

使用Python和Pyecharts创建交互式地图

《使用Python和Pyecharts创建交互式地图》在数据可视化领域,创建交互式地图是一种强大的方式,可以使受众能够以引人入胜且信息丰富的方式探索地理数据,下面我们看看如何使用Python和Pyec... 目录简介Pyecharts 简介创建上海地图代码说明运行结果总结简介在数据可视化领域,创建交互式地