我需要编写一个python代码来读取文本文件(file.txt)的内容并计算每个句子的平均单词数。(假设文件中每行只包含一个句子。)
我做了编码,我需要知道它是否可以通过其他方式提高效率。百万提前谢谢。 这是我的:
# This program reads contents of a .txt file and calulate
# the average number of words per sentence .
line_count=0
# open the file.txt for reading
content_file=open('file.txt','r')
# calculate the word count of the file
content=content_file.read()
words= content.split()
word_count=len(words)
# calculate the line count
for line in open('file.txt'):
line_count+=1
content_file.close()
# calculate the average words per line
average_words=word_count/line_count
# Display the result
print('The average word count per sentence is', int(average_words))
答案 0 :(得分:0)
无需迭代文件两次。只需通过以下行来更新计数::
lc, wc = 0, 0
with open('file.txt','r') as f:
for line in f:
lc += 1
wc += len(line.strip().split())
avg = wc / lc
答案 1 :(得分:0)
我的建议是,而不是使用for循环将内容拆分为'\ n'并找到数组的长度。
content_file =开放( 'file.txt的', 'R')
含量= content_file.read()
WORD_COUNT = LEN(content.split())
line_count = len(content.split('\ n'))
content_file.close()
average_words = WORD_COUNT / LINE_COUNT
print('每个句子的平均单词数为',int(average_words))
答案 2 :(得分:0)
以下代码将是高效的,因为我们一次只读取一次文件内容。
with open(r'C:\Users\lg49242\Desktop\file.txt','r') as content:
lineCount = 0
Tot_wordCount = 0
lines = content.readlines()
for line in lines:
lineCount = lineCount + 1
wordCount = len(line.split())
Tot_wordCount += wordCount
avg = Tot_wordCount / lineCount
print avg