python詞頻排序
⑴ python list的sorted排序問題,求大神解答
#-*-coding:utf-8-*-
withopen('word.txt','r')asf:
words=f.readlines()
word_dict={}
forwinwords:
w=w.rstrip()
w_count=word_dict.get(w,0)
ifw_count:
word_dict[w]+=1
else:
word_dict[w]=1
ww=sorted(word_dict.iteritems(),key=lambda(k,v):(v,k),reverse=True)
printww
⑵ 用Python統計詞頻
def statistics(astr):
# astr.replace("\n", "")
slist = list(astr.split("\t"))
alist = []
[alist.append(i) for i in slist if i not in alist]
alist[-1] = alist[-1].replace("\n", "")
return alist
if __name__ == "__main__":
code_doc = {}
with open("test_data.txt", "r", encoding='utf-8') as fs:
for ln in fs.readlines():
l = statistics(ln)
for t in l:
if t not in code_doc:
code_doc.setdefault(t, 1)
else:
code_doc[t] += 1
for keys in code_doc.keys():
print(keys + ' ' + str(code_doc[keys]))
⑶ Python 如何對輸出的詞頻結果按字母順序排序(NLTK)
importnltk
file_b=open('a.txt','r')
tokens=nltk.word_tokenize(file_b)
fdist1=nltk.FreqDist(tokens)
forkey,valinsorted(fdist1.iteritems())[:5]:
print("{1}:{0}".format(key,round(val/len(tokens),2)))
⑷ 如何用python和jieba分詞,統計詞頻
#!python3
#-*-coding:utf-8-*-
importos,codecs
importjieba
fromcollectionsimportCounter
defget_words(txt):
seg_list=jieba.cut(txt)
c=Counter()
forxinseg_list:
iflen(x)>1andx!=' ':
c[x]+=1
print('常用詞頻度統計結果')
for(k,v)inc.most_common(100):
print('%s%s%s%d'%(''*(5-len(k)),k,'*'*int(v/3),v))
if__name__=='__main__':
withcodecs.open('19d.txt','r','utf8')asf:
txt=f.read()
get_words(txt)
⑸ 如何用python對文章中文分詞並統計詞頻
1、全局變數在函數中使用時需要加入global聲明
2、獲取網頁內容存入文件時的編碼為ascii進行正則匹配時需要decode為GB2312,當匹配到的中文寫入文件時需要encode成GB2312寫入文件。
3、中文字元匹配過濾正則表達式為ur'[\u4e00-\u9fa5]+',使用findall找到所有的中文字元存入分組
4、KEY,Value值可以使用dict存儲,排序後可以使用list存儲
5、字元串處理使用split分割,然後使用index截取字元串,判斷哪些是名詞和動詞
6、命令行使用需要導入os,os.system(cmd)
⑹ 如何用python將詞頻中最高的前10個詞及出現的次數做出來並去掉重復的數字且進
# 利用字典進行處理
dic = {}
for word in speech:
if word not in dic:
dic[word] = 1
else:
dic[word] = dic[word] + 1
swd = sorted(dic.items(),key=operator.itemgetter(1),reverse=True)
⑺ 用python找出一篇文章中詞頻最高的20個單詞
import re
from collections import Counter
from matplotlib.pyplot import pie,show
f = 't.txt'
c = Counter(re.findall(r'(w{3,})',open(f).read().lower())).most_common(20)
pie([i[1] for i in c],labels=[i[0] for i in c])
show()
⑻ python怎麼升序和降序排序
python怎麼升序和降序排序
推薦:《python視頻教程》
1、首先打開cmd命令提示符,輸入指令「ipython」打開python的命令行工具:
2、在命令行中先定義一個變數number數組,裡面寫入幾個數,並用sorted函數對number排序並將排序的結果賦值給變數a,sorted函數第一個參數是要排序的參數,第二個是固定參數reverse表示倒序,True為開啟:
3、最後列印輸出a標量,就是降序輸出了:
更多相關問題,請關注PHP中文網!以上就是小編分享的關於python怎麼升序和降序排序的詳細內容希望對大家有所幫助,更多有關python教程請關注環球青藤其它相關文章!
⑼ python 如何同時按字母順序和詞頻排列詞頻統計的結果
fromcollectionsimportCounter
c=Counter(data)
b=sorted(c.most_common(),key=lambdax:(-x[1],x[0]))
print(b)
⑽ 如果用python查詢txt文檔按行的片語詞頻