计算单词,并对包含该N个单词的字符串排序

2024-04-27 00:03:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样的记录:

(bc9, de, viana=do=castelo)
(bc9, tomar o, aeroporto=de=pedras=rubras)
(arábia=saudita, em o, afeganistão)

我想数一数有多少次,哪个出现的次数最多,像这样:

bc9: 2 times. 

(bc9, de, viana=do=castelo)
(bc9, tomar o, aeroporto=de=pedras=rubras)

afeganistão: 1 times. 

(arábia=saudita, em o, afeganistão)

逗号之间的单词也不应该被计算在内。 下面是代码,它输出了一些连接符,我将删除这些连接符,但是在那之后,我想到了迭代输入并将包含单词的句子按它们出现的顺序分组

from Tkinter import Tk
from tkFileDialog import askopenfilename
import operator

Tk().withdraw() 
filename = askopenfilename() 
file = open(filename, "r+")
wordcount = {}
saida=open('saida.txt','w')
string = 'portugal] <civ> <*> prop m s @p<   ['
for line in file:
 line = line.replace("(", "")
 line = line.replace(")", "")
 line = line.replace(",", "")
 line = line.replace("=", " ")
 line = line.replace(string, "")
 saida.write(line)
saida.close()
file.close()
file=open("saida.txt","r")
for word in file.read().split():
 if word not in wordcount:
    wordcount[word] = 1
 else:
    wordcount[word] += 1
file.close
sorted_x = sorted(wordcount.items(), key=operator.itemgetter(1), reverse=True)
saida2=open('saida2.txt','w')
for key, value in sorted_x:
 saida2.write(key+':')
 saida2.write('\t')
 saida2.write(str(value) + '\n')
 print key, value

Tags: keyinimportlinedeopenwordcountreplace