collections

导入

import collections

1、计数器Counter

c = collections.Counter('nreyong')

print(c.items()) #打印键、值

print(c.keys()) #打印键

print(c.values()) #打印值

print(c.pop('a')) #删除键'a'并将值带回

c.clear() #清空c

d = collections.Counter('Python')

c.update(d) #将d中的值加到c上

c.update('abc')

del c['b'] #删除key为b的记录

for i in "nreyong": #添加元素
    c[i] += 1

print(c.most_common(n)) #出现最多的n组数

print(sorted(c.elements())) #排序后打印c中元素

c = collections.Counter(a = 3, b = 1)
d = collections.Counter(a = 1, b = 2)

print(c + d)                       # 相加
#Counter({'a': 4, 'b': 3})
print(c - d)                       # 相减,如果小于等于0,删去
#Counter({'a': 2})
print(c & d)                       # 求最小
#Counter({'a': 1, 'b': 1})
print(c | d)                       # 求最大
#Counter({'a': 3, 'b': 2})

2、默认字典defaultdict

import collections

d1 = collections.defaultdict(int)
d2 = collections.defaultdict(set)
d3 = collections.defaultdict(str)
d4 = collections.defaultdict(list)

print(d1[0])
print(d2[0])
print(d3[0])
print(d4[0])

''' 
输出:
0
set()

[]
'''

3、双端队列deque

import collections

# 创建
d = collections.deque()

# 右边添加
d.append('a')

# 左边添加
d.appendleft('b') # 在最左边添加一个元素,此时 d=deque(['b', 'a'])

# 右边添加列表中所有元素
d.extend(['c','d'])

# 左边添加列表中所有元素
d.extendleft(['e','f'])

# 弹出最右边元素
d.pop()

# 弹出最左边元素
d.popleft()

# 将最左边两个元素弹出并加到末尾(想想填正数会怎么样)
d.rotate(-2)

# 计数
d.count('a')

# 删除队列中的元素'c'
d.remove('c')

# 翻转
d.reverse()

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值