所以我需要编写一个程序来计算文件中所有单词的平均单词长度。到目前为止,我有这个,我完全失去了
newfile=input('Enter file name: ')
f=open(newfile,'r')
count1=0
count2=0
for line in f:
count1+=1
words=line.rstrip().split()
for word in words:
count2+=1
average=count1/count2
print('Average words per line: ',average)
答案 0 :(得分:5)
w i =单词i的长度
w avg =Σw i / N
with open(input('Enter file name: '),'r') as f:
w = [len(word) for line in f for word in line.rstrip().split(" ")]
w_avg = sum(w)/len(w)
答案 1 :(得分:1)
问题是你从未真正计算过一个单词的长度。 你可以做的是嵌套for循环:
for line in f:
for word in line.split():
然后递增计数器
count1+=1
count2+=len(word) # len(word) gives you the length exactly
最后,确保在计算平均值时进行浮点除法(而不是整数除法):
average=float(count2)/float(count1)
我复制并解释您的原始代码,以便您了解出现了什么问题:
newfile=input('Enter file name: ')
f=open(newfile,'r')
count1=0
count2=0
for line in f: # here, line is a string containing many words
count1+=1
words=line.rstrip().split() # here, "words" is a LIST of words
# you exit your for loop here
# so far, you looped over all your lines and did nothing with them
# and in your "words" variable, only the last line will be stored
for word in words: # here, "word" is a word
count2+=1 # you increment the counter with one, not with the word's length
average=count1/count2 # here, you perform integer division, and will lose the real part of the result
print('Average words per line: ',average)
答案 2 :(得分:0)
至于Python 2.7:
f = open('mercy.txt', 'r')
w = [len(word) for line in f for word in line.rstrip().split(" ")]
important to convert to float!
w_avg = float(sum(w))/float(len(w))
print 'Average word length: ', w_avg)
f.close()