Handwriting is still counting "python tips"? Try python class of counter bar


Today is met in the development of a very interesting question, how the number of occurrences statistics elements in a list.

Such scenes often appear in the statistics, are often used for corpus processing

How to count the number of elements in the sequence: Question

words = ['Beautiful', 'is', 'better', 'than', 'ugly', 'Explicit', 'is', 'better', 'than', 'implicit', 'Simple', 'is', 'better', 'than', 'complex', 'Complex', 'is', 'better', 'than', 'complicated', 'Flat', 'is', 'better', 'than', 'nested', 'Sparse', 'is', 'better', 'than', 'dense']

  • Thinking this problem generally normal process is to create a dictionary as a count operation

    # 计数子弹
    count_dict = {}
    words = ['Beautiful', 'is', 'better', 'than', 'ugly', 'Explicit', 'is', 'better', 'than', 'implicit', 'Simple', 'is',
             'better', 'than', 'complex', 'Complex', 'is', 'better', 'than', 'complicated', 'Flat', 'is', 'better', 'than',
             'nested', 'Sparse', 'is', 'better', 'than', 'dense']
    # 循环单词序列,如果不存在则创建初始值,如果存在则增加计数
    for word in words:
        if word in count_dict.keys():
            count_dict[word] += 1
        else:
            count_dict[word] = 0
    

    Such treatment can solve the problems we raised, there is a better approach more in line with python in it?

  • Collections module Counter class precisely to deal with this problem design

    # 引入counter
    >>> from collections import Counter
    # 一行代码即可搞定
    >>> word_counts = Counter(words)
    >>> word_counts
    Counter({'is': 6, 'better': 6, 'than': 6, 'Beautiful': 1, 'ugly': 1, 'Explicit': 1, 'implicit': 1, 'Simple': 1, 'complex': 1, 'Complex': 1, 'complicated': 1, 'Flat': 1, 'nested': 1, 'Sparse': 1, 'dense': 1})
    
    • After obtaining the results we can easily analyze the results, such as we want to get the highest ranking three words

      >>> word_counts.most_common(3)
      [('is', 6), ('better', 6), ('than', 6)]
      
    • Acquiring a count value of any word (Counter also a dictionary mapping underlayer)

      >>> word_counts['ugly']
      1
      
    • Manually increase the count is also very convenient

      >>> word_counts['ugly'] += 1
      >>> word_counts['ugly']
      2
      >>> word_counts['ugly'] = 8
      >>> word_counts['ugly']
      8
      
    • Add elements can also use the update syntax

      # 新增单词
      >>> more_words = ['Readability', 'counts', 'Special', 'cases', "aren't", 'special', 'enough', 'to', 'break', 'the', 'rules', 'Although', 'practicality', 'beats', 'purity', 'Errors', 'should', 'never', 'pass', 'silently', 'Unless', 'explicitly', 'silenced']
      # 直接使用update
      >>> word_counts.update(more_words)
      
      
      >>> word_counts
      Counter({'ugly': 8, 'is': 6, 'better': 6, 'than': 6, 'Beautiful': 1, 'Explicit': 1, 'implicit': 1, 'Simple': 1, 'complex': 1, 'Complex': 1, 'complicated': 1, 'Flat': 1, 'nested': 1, 'Sparse': 1, 'dense': 1, 'Readability': 1, 'counts': 1, 'Special': 1, 'cases': 1, "aren't": 1, 'special': 1, 'enough': 1, 'to': 1, 'break': 1, 'the': 1, 'rules': 1, 'Although': 1, 'practicality': 1, 'beats': 1, 'purity': 1, 'Errors': 1, 'should': 1, 'never': 1, 'pass': 1, 'silently': 1, 'Unless': 1, 'explicitly': 1, 'silenced': 1})
      
      
  • Counter other operations

    >>> a=Counter(words)
    >>> b=Counter(more_words)
    >>> a + b
    Counter({'is': 6, 'better': 6, 'than': 6, 'Beautiful': 1, 'ugly': 1, 'Explicit': 1, 'implicit': 1, 'Simple': 1, 'complex': 1, 'Complex': 1, 'complicated': 1, 'Flat': 1, 'nested': 1, 'Sparse': 1, 'dense': 1, 'Readability': 1, 'counts': 1, 'Special': 1, 'cases': 1, "aren't": 1, 'special': 1, 'enough': 1, 'to': 1, 'break': 1, 'the': 1, 'rules': 1, 'Although': 1, 'practicality': 1, 'beats': 1, 'purity': 1, 'Errors': 1, 'should': 1, 'never': 1, 'pass': 1, 'silently': 1, 'Unless': 1, 'explicitly': 1, 'silenced': 1})
    
    >>> a - b
    Counter({'is': 6, 'better': 6, 'than': 6, 'Beautiful': 1, 'ugly': 1, 'Explicit': 1, 'implicit': 1, 'Simple': 1, 'complex': 1, 'Complex': 1, 'complicated': 1, 'Flat': 1, 'nested': 1, 'Sparse': 1, 'dense': 1})
    

to sum up

1. 当面对任何数据制表或者计数是使用counter会比手写字典计数算法更有效率

"The world is like a huge circus, it makes you excited, made me fear, because I know that will always be limited Afterwards gentle, infinitely sad." - Charlie Chaplin

Here Insert Picture Description

Published 13 original articles · won praise 8 · views 20000 +

Guess you like

Origin blog.csdn.net/JOHNEW/article/details/104916564