zoukankan      html  css  js  c++  java
  • python中的collections模块

    >>> c = Counter('abcdeabcdabcaba')  # count elements from a string
     |  
     |  >>> c.most_common(3)                # three most common elements
     |  [('a', 5), ('b', 4), ('c', 3)]
     |  >>> sorted(c)                       # list all unique elements
     |  ['a', 'b', 'c', 'd', 'e']
     |  >>> ''.join(sorted(c.elements()))   # list elements with repetitions
     |  'aaaaabbbbcccdde'
     |  >>> sum(c.values())                 # total of all counts
     |  15
     |  
     |  >>> c['a']                          # count of letter 'a'
     |  5
     |  >>> for elem in 'shazam':           # update counts from an iterable
     |  ...     c[elem] += 1                # by adding 1 to each element's count
     |  >>> c['a']                          # now there are seven 'a'
     |  7
     |  >>> del c['b']                      # remove all 'b'
     |  >>> c['b']                          # now there are zero 'b'
     |  0
     |  
     |  >>> d = Counter('simsalabim')       # make another counter
     |  >>> c.update(d)                     # add in the second counter
     |  >>> c['a']                          # now there are nine 'a'
     |  9
     |  
     |  >>> c.clear()                       # empty the counter
     |  >>> c
     |  Counter()
  • 相关阅读:
    Fiddler-代理-过滤-弱网测试
    POJ2186 Popular Cows
    POJ3264 Balanced Lineup
    多模式串字符串匹配模板题
    Intersecting Lines
    实现堆结构
    OpenJuege 兔子与星空
    拓扑排序
    POJ3635 Full Tank?
    OpenJudge Cartesian Tree
  • 原文地址:https://www.cnblogs.com/lmt921108/p/8029407.html
Copyright © 2011-2022 走看看