自己做网站 需要会什么6做网站
news/
2025/9/27 21:04:07/
文章来源:
自己做网站 需要会什么,6做网站,新网域名自助管理平台,烟台网站建设兼职Python中的collections模块 文章目录 Python中的collections模块1.Counter对象2.deque对象3.defaultdict对象4.namedtuple5.OrderedDictReference Python中的
collections提供许多容器数据类型#xff0c;这个模块实现了一些专门化的容器#xff0c;提供了对Python的通用内建…Python中的collections模块 文章目录 Python中的collections模块1.Counter对象2.deque对象3.defaultdict对象4.namedtuple5.OrderedDictReference Python中的
collections提供许多容器数据类型这个模块实现了一些专门化的容器提供了对Python的通用内建容器
dict、
list、
set和
tuple的补充。 容器功能namedtuple()一个工厂函数用于创建元组的子类deque类似列表的队列但append和pop在其两端的速度都极快ChainMap类似字典的类用于创建包含多个映射的单个视图Counter用于计数hashable对象的字典子类OrderedDict字典的子类能记住条目被添加的顺序defaultDict字典的子类通过调用用户指定的工厂函数为键提供默认值UserDict封装了字典对象简化了字典子类化UserList封装了列表对象简化了列表子类化UserString封装了字符串对象简化了字符串子类化
1.Counter对象
Counter是一个计数器工具目的是为了快速记账例如
from collections import Counter
cnt: Counter Counter()
words [apple, banana, apple, orange, banana, apple]
for word in words:cnt[word] 1
print(cnt)Counter({apple: 3, banana: 2, orange: 1})Counter可以将元素储存为字典的键而它们的计数储存为字典的值。计数可以为任何整数包括零或负的计数值。
Counter可以使用如下的方式来进行初始化
c Counter() # a new, empty counter
c Counter(gallahad) # a new counter from an iterable
c Counter({red: 4, blue: 2}) # a new counter from a mapping
c Counter(cats4, dogs8) # a new counter from keyword args如果查询的值不在Counter中则会返回0而不是像字典以用返回一个KeyError。
c Counter(cats4, dogs8) # a new counter from keyword args
c[birds]0设置一个计数为0并不会从Counter中删除它若要删除它则使用del
c Counter(cats4, dogs8) # a new counter from keyword args
c[cats] 0
del c[dogs]
print(c)Counter({cats: 0})2.deque对象
deque是一种双端队列[double end queue]我们可以利用这个双端队列来实现数据结构中的栈stack和队列queue。deque支持以下方法
方法功能append(x)将x添加到右端appendleft(x)将x添加到左端clear()移除所有元素使其长度为0copy()创建一份浅拷贝count(x)统计deque中元素等于x的个数extend(iterable)拓展deque的右侧通过添加iterable参数中的元素extendleft(iterable)拓展deque的左侧通过添加iterable参数中的元素index(x[, start[, stop]])返回x在deque中的位置在索引start之后在索引stop之前insert(i, x)在i的位置插入xpop()移除并且返回deque最右端的元素popleft()移除并且返回deque最左端的元素remove(value)移除找到的第一个valuereverse()将deque逆序排列rotate(n1)向右循环移动n步如果n是负数则向左循环如果deque不是空的向右循环移动一步就等价于 d.appendleft(d.pop()) 向左循环一步就等价于 d.append(d.popleft()) 。maxlen返回deque的最大尺寸
以下是一个使用示例
from collections import deque
d: deque deque(efg) # make a new deque with three items
for item in d:print(item)e
f
gd.append(h) # add a new entry to the right side
d.appendleft(d) # add a new entry to the left side
print(d) # show the representation of the dequedeque([d, e, f, g, h])print(d.pop()) # return and remove the rightmost item
print(d.popleft()) # return and remove the leftmost item
print(d)h
d
deque([e, f, g])print(d[0]) # peek at leftmost item
print(d[-1]) # peek at rightmost iteme
gd.extend(hij) # add multiple elements to right at once
d.extendleft(cba) # add multiple elements to left at once
print(d)deque([a, b, c, e, f, g, h, i, j])d.rotate(1) # right rotation
print(d)deque([j, a, b, c, e, f, g, h, i])d.rotate(-1) # left rotation
print(d) deque([a, b, c, e, f, g, h, i, j])3.defaultdict对象
在使用dict时如果引用的key不存在就会抛出KeyError如果希望key不存在的时候返回一个默认值就可以使用defaultdict:
from collections import defaultdict
d: dict defaultdict(lambda: N/A) # 设置不存在的键返回N/A
d[key1] abc
print(d[key1]) # 查询存在的键
print(d[key2]) # 查询不存在的键abc
N/A4.namedtuple
我们直到tuple可以表示不变的集合但是我们通常不能直接通过一个集合来看出其表示的含义比如我们定义了一个坐标:
p (1, 2)但是我们看到(1,2)很难直到这个tuple是用于表示一个坐标。这时namedtuple就发挥作用了:
from collections import namedtuple
Point namedtuple(Point, [x, y])
p Point(11, y22) # instantiate with positional or keyword arguments
print(p[0] p[1]) # indexable like the plain tuple (11, 22)
x, y p # unpack like a regular tuple
print(x)
print(y)
print(p.x p.y) # fields also accessible by name33
11
22
33namedtuple是一个函数它用来创建一个自定义的tuple对象并且规定了tuple元素的个数并可以用属性而不是索引来引用tuple的某个元素。这样一来我们使用namedtuple可以很方便地定义一种数据类型。
5.OrderedDict
OrderedDict是一种保存key添加的顺序的dictOrdereddict的key会按照插入的顺序进行排序而不是把key本身进行排序。
from collections import OrderedDict
od OrderedDict()
od[a] 1
od[b] 2
od[c] 3
print(od)OrderedDict([(a, 1), (b, 2), (c, 3)])Reference
python标准库说明 廖雪峰collections
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/919938.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!