Advertisement

python输入英文句子、找最长单词_在输入fi中找出句子中的最大和最小单词数

阅读量:

我有一个问题,要求我找出文本文件中单词的最小和最大数量。我已经完成了五个问题中的三个,剩下的两个是关于最小值和最大值的问题,我对此没有任何解决办法。以下是我的代码:感谢您的帮助lines, blanklines, sentences, words = 0, 0, 0, 0,

print '-'

full_text = 'input.txt'

empty_text = 'output.txt'

text_file = open(full_text, 'r')

out_file = open(empty_text, "w")

for line in text_file:

print line

lines += 1

if line.startswith('\n'):

blanklines += 1

else:

assume that each sentence ends with . or ! or ?

so simply count these characters

sentences += line.count('.') + line.count('!') + line.count('?')

create a list of words

use None to split at any whitespace regardless of length

so for instance double space counts as one space

word total count

words += len(line.split())

average = float(words) / float(sentences)

text_file.close()

out_file.close()

######## T E S T P R O G R A M ########

print

print '-'

print "Total number of sentences in the input file : ", sentences

print "Total number of words in the input file : ", words

print "Average number of words per sentence : ", average

全部评论 (0)

还没有任何评论哟~