python 中怎么統(tǒng)計單詞并排序,針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

lines_count = 0
words_count = 0
chars_count = 0
words_dict = {}
lines_list = []
c1 = ['!', '?', ';', '.', ',']
all_line = ''
import collections
with open('c:\invalid.txt', 'r') as f:
for line in f:
line = line.strip('\n')
lines_count = lines_count + 1
all_line=all_line+line.lower()
# print line
#print all_line
for i in c1:
all_line=all_line.replace(i,' ')
# print all_line
str1=all_line.split(' ')
for i in str1:
if i not in words_dict:
words_dict[i] = 1
else:
words_dict[i] = words_dict[i] + 1
c1=words_dict.items()
c2=[]
for i in c1:
c2.append((i[1],i[0]))
d2={}
d2={}
for i in c2:
d2[i[0]]=i[1]
def mySort(a):
lo = len(a) - 1
i = 0
while i < lo + 1:
j = lo
while j >= i:
if (a[j - 1] > a[j]):
a[j], a[j - 1] = a[j - 1], a[j]
j = j - 1
i = i + 1 if __name__ == '__main__':
for i in sorted(d2.keys()):
print d2[i], ':', i 關(guān)于python 中怎么統(tǒng)計單詞并排序問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道了解更多相關(guān)知識。
分享文章:python中怎么統(tǒng)計單詞并排序-創(chuàng)新互聯(lián)
鏈接URL:http://chinadenli.net/article32/hggpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護、Google、做網(wǎng)站、微信小程序、小程序開發(fā)、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容