欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Python用三種方式統(tǒng)計(jì)詞頻的方法

 更新時(shí)間:2019年07月29日 11:48:18   作者:little_people  
這篇文章主要介紹了Python用三種方式統(tǒng)計(jì)詞頻,每種方法給大家介紹的非常詳細(xì),需要的朋友可以參考下

三種方法:

①直接使用dict

②使用defaultdict

③使用Counter

 ps:`int()`函數(shù)默認(rèn)返回0

 ①dict

text = "I'm a hand some boy!"
frequency = {}
for word in text.split():
  if word not in frequency:
    frequency[word] = 1
  else:
    frequency[word] += 1

 ②defaultdict

import collections
frequency = collections.defaultdict(int)
text = "I'm a hand some boy!"
for word in text.split():
  frequency[word] += 1

 ③Counter

import collections
text = "I'm a hand some boy!"
frequency = collections.Counter(text.split())

總結(jié)

以上所述是小編給大家介紹的Python用三種方式統(tǒng)計(jì)詞頻的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

最新評(píng)論