Python collections.Counter()用法
生活随笔
收集整理的這篇文章主要介紹了
Python collections.Counter()用法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Python collections.Counter用法
- 什么是collections
- Counter
- Counter操作
- 例子
什么是collections
collections在python官方文檔中的解釋是High-performance container datatypes,直接的中文翻譯解釋高性能容量數據類型。
它總共包含五種數據類型:
其中Counter中文意思是計數器,也就是我們常用于統計的一種數據類型,在使用Counter之后可以讓我們的代碼更加簡單易讀。
Counter
我們先看一個簡單的例子:
#統計詞頻 colors = ['red', 'blue', 'red', 'green', 'blue', 'blue'] result = {} for color in colors:if result.get(color)==None:result[color]=1else:result[color]+=1 print (result) #{'red': 2, 'blue': 3, 'green': 1}下面我們看用Counter怎么實現:
from collections import Counter colors = ['red', 'blue', 'red', 'green', 'blue', 'blue'] c = Counter(colors) print (dict(c))顯然代碼更加簡單了,也更容易讀和維護了。
Counter操作
可以創建一個空的Counter:
cnt = Counter()之后在空的Counter上進行一些操作。
也可以創建的時候傳進去一個迭代器(數組,字符串,字典等):
判斷是否包含某元素,可以轉化為dict然后通過dict判斷,Counter也帶有函數可以判斷:
c = Counter(['eggs', 'ham']) c['bacon'] # 不存在就返回0 #0刪除元素:
c['sausage'] = 0 # counter entry with a zero count del c['sausage']獲得所有元素:
c = Counter(a=4, b=2, c=0, d=-2) list(c.elements()) #['a', 'a', 'a', 'a', 'b', 'b']查看最常見出現的k個元素:
Counter('abracadabra').most_common(3) #[('a', 5), ('r', 2), ('b', 2)]Counter更新:
c = Counter(a=3, b=1) d = Counter(a=1, b=2) c + d # 相加 #Counter({'a': 4, 'b': 3}) c - d # 相減,如果小于等于0,刪去 #Counter({'a': 2}) c & d # 求最小 #Counter({'a': 1, 'b': 1}) c | d # 求最大 #Counter({'a': 3, 'b': 2})例子
例子:讀文件統計詞頻并按照出現次數排序,文件是以空格隔開的單詞的諸多句子:
from collections import Counter lines = open("./data/input.txt","r").read().splitlines() lines = [lines[i].split(" ") for i in range(len(lines))] words = [] for line in lines:words.extend(line) result = Counter(words) print (result.most_common(10))當需要統計的文件比較大,使用read()一次讀不完的情況:
from collections import Counter result = Counter() with open("./data/input.txt","r") as f:while True:lines = f.read(1024).splitlines()if lines==[]:breaklines = [lines[i].split(" ") for i in range(len(lines))]words = []for line in lines:words.extend(line)tmp = Counter(words)result+=tmpprint (result.most_common(10))具體可以參考 https://docs.python.org/2/library/collections.html#collections.Counter.most_common
總結
以上是生活随笔為你收集整理的Python collections.Counter()用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenJudge百炼-2965-玛雅历
- 下一篇: 我迟早被这些AI绘画笑死...