Hello all,
Quick question-i am trying to write a programs whcih calculates the average of the numeric values of words that appear multiple times (if a word appears more than once then i want the average of its values). I do not get an error but instead only the sum of each word's value.This is the input of the program:
index 388.315813
index 311.214286
syndrome 289.708333
factor 184.246753
loss 168.578313
support 153.125000
activity 140.357143
circumference 136.500000
disease 122.529412
tissue 105.931507
So the output will have 9 lines since it will integrate the first two and find their average value. My problem is that my code doesn't work properly. I mean, i do get any errors but it doesn't seem to perform the dividion in order to find the average value.
This is what i ve written so far:
f=open('example.txt','r')
for line in f:
words = line.split()
if len(words) == 2:
count = 1
word = words[0]
cvalue = float(words[1])
if word not in wordsdict:
wordsdict[word] = cvalue
else:
count += 1
wordsdict[word] = sum([cvalue,wordsdict[word]])
average = wordsdict[word] / count
elif len(words) == 3:
word = words[0] + " " + words [1]
cvalue = float(words[2])
if word not in wordsdict:
wordsdict[word] = cvalue
else:
count += 1
wordsdict[word] = sum([cvalue,wordsdict[word]])
average = wordsdict[word] / count
Any thoughts of why this is happening? I may miss something because i am new in python:$