Advertisement

Python标准库collections模块中的Counter类更新功能

阅读量:

建立一个 Counter 对象

复制代码
    """ 构造一个空 Counter 对象 """ 
    # c = Counter()                           # a new, empty counter
    
    """ 三种构造非空 Counter 对象的方法,返回结果的顺序与输入顺序相同 """
    # 1、a new counter from an iterable
    # c = Counter('bccabc')  # Counter({'b': 2, 'c': 3, 'a': 1})                 
    
    # 2、a new counter from a mapping
    # c = Counter({'a': 1, 'b': 2, 'c': 3})  # Counter({'a': 1, 'b': 2, 'c': 3})      
    
    # 3、a new counter from keyword args
    c = Counter(a=1, b=2, c=3)  # Counter({'a': 1, 'b': 2, 'c': 3})             
    
    
    AI写代码python

全部评论 (0)

还没有任何评论哟~