collections 模块

collections 模块

标签(空格分隔): pythoncook笔记


collections.deque

用法:—— 创造简单队列 ——–

使用deque(maxlen=N)会构建一个大小为N的简单队列,当新的元素加入并且这个队列已满时,最老的元素会被删除。

>>> q = deque(maxlen=3)
>>> q.append(1)
>>> q.append(2)
>>> q.append(3)
>>> q
deque([1, 2, 3], maxlen=3)
>>> q.append(4)
>>> q
deque([2, 3, 4], maxlen=3)
>>> q.append(5)
>>> q
deque([3, 4, 5], maxlen=3)

不设参数maxlen可以生成无限的队列,对队列的操作

q.append(1) # 在队列最右添加
q.pop(1)  # 在队列最右删除
q.appendleft(1)  # 在队列最左添加
q.popleft(1) # 在队列最右删除

collections.defaultdict

用法:——– 创造可以一键映射多值的字典 ———–

>>> from collections import defaultdict
>>> d = defaultdict(list)
>>> d
defaultdict(<class 'list'>, {})
>>> d['a'].append(1)
>>> d
defaultdict(<class 'list'>, {'a': [1]})
>>> type(d)
<class 'collections.defaultdict'>
>>> d['a'].append(2)
>>> d
defaultdict(<class 'list'>, {'a': [1, 2]})
>>> d['b'].append(3)
>>> d
defaultdict(<class 'list'>, {'b': [3], 'a': [1, 2]})

collections.OrderedDict

用法: ——— 字典排序 ————–

from collections import OrderedDict

d = OrderedDict()
d['foo'] = 1
d['bar'] = 2
d['spam'] = 3
d['grok'] = 4
# Outputs "foo 1", "bar 2", "spam 3", "grok 4"
for key in d:
    print(key, d[key])

它将使字典保持你插入元素的顺序

注意 该方法用来支持数据量小的字典较好,它的大小为普通字典的两倍。而且因为它储存时用的双向链表,每次当一个新的元素插入进来的时候,它会被放到链表的尾部,对于一个已经存在的键的重复赋值不会改变键的顺序。

>>> from collections import OrderedDict
>>> d = OrderedDict()
>>> d['foo'] = 1
>>> d['bar'] = 2
>>> d['foo'] = 3
>>> d
OrderedDict([('foo', 3), ('bar', 2)])

collections.Counter

用法: ——– 找出列表中出现次数最多的元素 ————–

words = [
    'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
    'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',
    'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into',
    'my', 'eyes', "you're", 'under'
]
from collections import Counter
word_counts = Counter(words)
print(word_counts)
# Output {'eyes': 8, 'the': 5, 'look': 4, 'my': 3, 'into': 3, 'around': 2, 'not': 1, 'under': 1, "don't": 1, "you're": 1}
# 出现频率最高的3个单词
top_three = word_counts.most_common(3)
print(top_three)
# Outputs [('eyes', 8), ('the', 5), ('look', 4)]

作为输入, Counter对象可以接受任意的由可哈希(hashable)元素构成的序列对象。在底层实现上,一个 Counter对象就是一个字典,将元素映射到它出现的次数上。

令人惊喜的是,Counter对象可以跟数学运算符进行结合

>>> a = Counter(words)
>>> b = Counter(morewords)
>>> a
Counter({'eyes': 8, 'the': 5, 'look': 4, 'into': 3, 'my': 3, 'around': 2,
"you're": 1, "don't": 1, 'under': 1, 'not': 1})
>>> b
Counter({'eyes': 1, 'looking': 1, 'are': 1, 'in': 1, 'not': 1, 'you': 1,
'my': 1, 'why': 1})
>>> # Combine counts
>>> c = a + b
>>> c
Counter({'eyes': 9, 'the': 5, 'look': 4, 'my': 4, 'into': 3, 'not': 2,
'around': 2, "you're": 1, "don't": 1, 'in': 1, 'why': 1,
'looking': 1, 'are': 1, 'under': 1, 'you': 1})
>>> # Subtract counts
>>> d = a - b
>>> d
Counter({'eyes': 7, 'the': 5, 'look': 4, 'into': 3, 'my': 2, 'around': 2,
"you're": 1, "don't": 1, 'under': 1})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值