Created
          May 16, 2017 15:39 
        
      - 
      
- 
        Save Everfighting/392606d0ae31a8adf4e298237808d246 to your computer and use it in GitHub Desktop. 
    统计序列中元素出现的频度,及最高的三个值
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | from random import randint | |
| data = [randint(0,20) for _ in range(30)] | |
| c = dict.fromkeys(data,0) | |
| for x in data: | |
| c[x] += 1 | |
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | from collections import Counter | |
| # 统计词频 | |
| c2 = Counter(data) | |
| # 出现频度最高的n个值 | |
| c2.most_common(3) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import re | |
| txt = open('CodingStyle').read() | |
| # 内容分割,进行词频统计 | |
| c3 = Counter(re.split('\W+',txt)) | |
| # 统计词频最高10个词 | |
| c3.most_common(10) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment