【发布时间】:2017-09-13 15:36:12
【问题描述】:
该程序的这一点是计算单词的出现次数,同时忽略标点符号、冠词和连词。所需的输出是使用的前 15 个单词和使用的后 15 个单词的列表,但未显示它们的出现。我是初学者,任何帮助将不胜感激。谢谢!
# This program reads a text file, performs a content analysis
# and prints both a top 15 and a bottom 15 report
name = input('Enter name of file: ')
# Clean Function
def clean(s):
punctuations = ["!","@","#","$"]
art_con = ['the','a','an','some','and','but','or','nor','for']
for each in punctuations:
s = s.replace(each,"")
words = s.split()
resultwords = [word for word in words if word.lower() not in art_con]
result= ''.join(resultwords)
return result
# Analyze Function
def analyze(name):
print('Reading',name,'for analysis...')
print('===========================')
print('Creating content analysis dictionary...')
r = open(name, 'r')
s = r.read()
result = clean(s)
count = dict((x,result.count(x)) for x in set(result))
print('Analysis complete!')
print('===================')
return count
count = analyze(name)
# turn dictionary into a list of tuples to sort
def function(count):
list1 = []
for key in count:
t = (count[key],key)
list1.append((t))
list1.sort()
result = [list1[i] for i in range(len(list1))]
t15 = result[0:15]
b15 = result[-15:0]
print("The top 15 words are ",t15)
print("The bottom 15 words are ",b15)
#Main Function
def main():
count = analyze(name)
function(count)
main()
【问题讨论】:
-
欢迎来到 StackOverflow。请阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布 MCVE 代码并准确描述问题之前,我们无法有效地帮助您。
-
好的……你的问题是什么?
标签: python list file dictionary tuples