从文件中读取字数并计算每个句子的平均值的有效方法

问题描述 投票:-1回答:3

我需要编写一个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))
python file word-count line-count
3个回答
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

0
投票

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

open the file.txt for reading

content_file =开放( 'file.txt的', 'R')

calculate the word count of the file

含量= content_file.read()

WORD_COUNT = LEN(content.split())

line_count = len(content.split('\ n'))

content_file.close()

calculate the average words per line

average_words = WORD_COUNT / LINE_COUNT

Display the result

print('每个句子的平均字数是',int(average_words))


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

打印八月

© www.soinside.com 2019 - 2024. All rights reserved.