日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python collections.Counter()用法

發布時間:2023/12/18 python 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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上進行一些操作。
也可以創建的時候傳進去一個迭代器(數組,字符串,字典等):

c = Counter('gallahad') # 傳進字符串 c = Counter({'red': 4, 'blue': 2}) # 傳進字典 c = Counter(cats=4, dogs=8) # 傳進元組

判斷是否包含某元素,可以轉化為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()用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。