• Python内建模块collections


    collections

    • namedtuple():工厂函数用于创建具有命名字段的元组子类
    • deque:类似list的容器,可在两端快速增加或删除
    • ChainMap:类似dict的类,用于创建多个映射的单个视图
    • Counter:字典的子类,用于统计计数
    • OrderedDict:字典子类,记录添加的顺序条目
    • defaultdict:调用工厂函数提供缺少值的字典子类

    Counter

    # 统计列表中的元素个数
    >>> from collections import Counter
    >>> cnt = Counter()
    >>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
    ...     cnt[word] += 1 
    ... 
    >>> cnt
    Counter({'blue': 3, 'red': 2, 'green': 1})
    # 统计文件中单词出现的个数
    >>> import re
    >>> words = re.findall(r'w+', open('hamlet.txt').read().lower())
    >>> Counter(words).most_common(10)	# 出现次数最多的10个
    [('you', 11), ('of', 8), ('i', 6), ('a', 6), ('in', 6), ('and', 5), ('past', 4), ('into', 4), ('t', 4), ('my', 4)]
    

    创建Counter()对象

    >>> c = Counter()
    >>> c = Counter('gallahad')
    >>> c = Counter({'red': 4, 'blue': 2})
    >>> c = Counter(cat=4, dogs=8)
    

    从counter中获取不存在的元素时返回0

    >>> c = Counter(['eggs', 'ham'])
    >>> c['bacon']
    0
    

    将counter中元素的计数设为0并没有将元素移除,使用del才会移除

    >>> c['eggs']
    1
    >>> c['eggs'] = 0
    >>> c
    Counter({'ham': 1, 'eggs': 0})
    >>> del c['eggs']
    >>> c
    Counter({'ham': 1})
    

    Counter()的方法

    elements() :按照随机顺序列出所有元素,每个元素的次数为它统计的数字。如果次数小于1则会忽略。

    >>> c = Counter(a=4, b=2, c=0, d=-2)
    >>> sorted(c.elements())
    ['a', 'a', 'a', 'a', 'b', 'b']
    

    most_common([n]) :列出出现次数最多的n个元素,如果n为空,则列出所有的。

    >>> Counter('abracadabra').most_common(3)
    [('a', 5), ('r', 2), ('b', 2)]
    

    subtract([iterable-or-mapping]) :将counter中的对应元素统计值相减

    >>> c = Counter(a=4, b=2, c=0, d=-2)
    >>> d = Counter(a=1, b=2, c=3, d=4)
    >>> c.subtract(d)
    >>> c
    Counter({'a': 3, 'b': 0, 'c': -3, 'd': -6})
    

    update([iterable-or-mapping]) :将counter中的对应元素统计值相加

    >>> c = Counter(a=4, b=2, c=0, d=-2)
    >>> d = Counter(a=1, b=2, c=3, d=4)
    >>> c.update(d)
    >>> c
    Counter({'a': 5, 'b': 4, 'c': 3, 'd': 2})
    

    常用操作

    sum(c.values())        			# 统计所有元素的计数
    c.clear()                    	# 重置一个Counter()
    list(c)                         # 列出唯一的元素
    set(c)                          # 转化为set
    dict(c)                         # 转化为字典
    c.items()                       # 转化为(elem, cnt)对的list
    Counter(dict(list_of_pairs))    # 从(elem, cnt)对的list转化
    c.most_common()[:-n-1:-1]       # n个最少的元素
    +c                              # 移除0和负数计数
    

    Counter()间的+,-,&,|操作

    >>> c = Counter(a=3, b=1)
    >>> d = Counter(a=1, b=2)
    >>> c + d                       # 相加
    Counter({'a': 4, 'b': 3})
    >>> c - d                       # 相减(结果只保留正的计数)
    Counter({'a': 2})
    >>> c & d                       # 取较小计数min(c[x], d[x]) 
    Counter({'a': 1, 'b': 1})
    >>> c | d                       # 取较大计数max(c[x], d[x])
    Counter({'a': 3, 'b': 2})
    

    单个Counter()的+,-操作

    >>> c = Counter(a=2, b=-4)
    >>> +c
    Counter({'a': 2})
    >>> -c
    Counter({'b': 4})
    
  • 相关阅读:
    安卓app_sl3_18_19列表视图
    安卓app_sl3_17列表选择框Spinner
    安卓app_sl3_15复选框按钮示范
    eclipse如何新建安卓app项目程序
    安卓app_sl3_21日期拾取器,为时间拾取器设置监听器
    eclipse 安卓app开发环境搭建
    android从入门到精通
    安卓app_sl3_16图像视图线性布局
    安卓app_sl3_22计时器_显示当前时间_系统时间
    二叉树及其三种遍历
  • 原文地址:https://www.cnblogs.com/suraer/p/8435937.html
Copyright © 2020-2023  润新知