-1

我需要编写一个 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))
4

3 回答 3

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

打印平均值

于 2017-12-19T07:22:13.887 回答
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
于 2017-12-19T07:14:18.623 回答
0

我的建议是,不要使用 for 循环将内容拆分为 '\n' 并找到数组的长度。

打开file.txt进行阅读

content_file=open('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))

于 2017-12-19T07:18:39.250 回答