我有这样的条目:
(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
答案 0 :(得分:0)
from collections import Counter
zipped_entries = zip(*entries)
Counter(zipped_entries[0] + zipped_entries[2])
假设您有固定数量的条目(3)。应该有足够的代码来帮助您入门。