Python每日一练(1):计算文件夹内各个文章中出现次数最多的单词
生活随笔
收集整理的這篇文章主要介紹了
Python每日一练(1):计算文件夹内各个文章中出现次数最多的单词
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#coding:utf-8
import os,repath = 'test'
files = os.listdir(path)def count_word(words):dic = {}max = 0marked_key = ''#計算每個單詞出現的次數for word in words:if dic.has_key(word) is False:dic[word] = 1else:dic[word] = dic[word] +1#每個字典的值之間做比較,得出最大的那個數字for key,value in dic.items():if dic[key] > max:max = dic[key]marked_key = key#跳出for循環打印出單詞和單詞出現的次數print(marked_key,max)for f in files:with open(os.path.join(path,f)) as diary:words = re.findall("[a-zA-Z]+'*-*[a-zA-z]", diary.read())count_word(words)
#has_key(key) : 函數用于判斷鍵是否存在于字典中,如果鍵在字典dict里返回true,否則返回false。這里用于判斷字典內的鍵是否出現過。
#has_key(key) : 函數用于判斷鍵是否存在于字典中,如果鍵在字典dict里返回true,否則返回false。這里用于判斷字典內的鍵是否出現過。
?
這個是Git上的Python每日一聯小項目,我就不提交到那邊了,寫到這里來。
項目地址:https://github.com/Yixiaohan/show-me-the-code
轉載于:https://www.cnblogs.com/sxcmos/p/5143771.html
總結
以上是生活随笔為你收集整理的Python每日一练(1):计算文件夹内各个文章中出现次数最多的单词的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 0119——UITextField
- 下一篇: python3下的paramiko 安装