失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python输入英文句子 找最长单词_在输入fi中找出句子中的最大和最小单词数

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

时间:2020-07-30 00:35:43

相关推荐

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

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

print '-' * 50

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 '-' * 50

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

如果觉得《python输入英文句子 找最长单词_在输入fi中找出句子中的最大和最小单词数》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。