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

歡迎訪問 生活随笔!

生活随笔

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

python

python常用单词汇总_在.txt文件中找到最常用单词的Python程序必须打印word及其连接...

發布時間:2025/3/19 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python常用单词汇总_在.txt文件中找到最常用单词的Python程序必须打印word及其连接... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

現在,我有一個函數來替換countChars函數def countWords(lines):

wordDict = {}

for line in lines:

wordList = lines.split()

for word in wordList:

if word in wordDict: wordDict[word] += 1

else: wordDict[word] = 1

return wordDict

但當我運行這個程序時,它會吐出這種討厭的東西(這只是一個例子,大約有兩頁文字旁邊有大量的數字)before 1478

battle-field 1478

as 1478

any 1478

altogether 1478

all 1478

ago 1478

advanced. 1478

add 1478

above 1478

雖然很明顯這意味著代碼足夠運行,但我沒有得到我想要的。

它需要打印文件中每個單詞的次數(gb.txt,這是葛底斯堡地址)

很明顯文件中的每個單詞都不在1478次。。

我對編程很陌生,所以我有點困惑。。from __future__ import division

inputFileName = 'gb.txt'

def readfile(fname):

f = open(fname, 'r')

s = f.read()

f.close()

return s.lower()

def countChars(t):

charDict = {}

for char in t:

if char in charDict: charDict[char] += 1

else: charDict[char] = 1

return charDict

def findMostCommon(charDict):

mostFreq = ''

mostFreqCount = 0

for k in charDict:

if charDict[k] > mostFreqCount:

mostFreqCount = charDict[k]

mostFreq = k

return mostFreq

def printCounts(charDict):

for k in charDict:

#First, handle some chars that don't show up very well when they print

if k == '\n': print '\\n', charDict[k] #newline

elif k == ' ': print 'space', charDict[k]

elif k == '\t': print '\\t', charDict[k] #tab

else: print k, charDict[k] #Normal character - print it with its count

def printAlphabetically(charDict):

keyList = charDict.keys()

keyList.sort()

for k in keyList:

#First, handle some chars that don't show up very well when they print

if k == '\n': print '\\n', charDict[k] #newline

elif k == ' ': print 'space', charDict[k]

elif k == '\t': print '\\t', charDict[k] #tab

else: print k, charDict[k] #Normal character - print it with its count

def printByFreq(charDict):

aList = []

for k in charDict:

aList.append([charDict[k], k])

aList.sort() #Sort into ascending order

aList.reverse() #Put in descending order

for item in aList:

#First, handle some chars that don't show up very well when they print

if item[1] == '\n': print '\\n', item[0] #newline

elif item[1] == ' ': print 'space', item[0]

elif item[1] == '\t': print '\\t', item[0] #tab

else: print item[1], item[0] #Normal character - print it with its count

def main():

text = readfile(inputFileName)

charCounts = countChars(text)

mostCommon = findMostCommon(charCounts)

#print mostCommon + ':', charCounts[mostCommon]

#printCounts(charCounts)

#printAlphabetically(charCounts)

printByFreq(charCounts)

main()

總結

以上是生活随笔為你收集整理的python常用单词汇总_在.txt文件中找到最常用单词的Python程序必须打印word及其连接...的全部內容,希望文章能夠幫你解決所遇到的問題。

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