Hello all,
I have a question. You see i have a file which contains data in this format:
index 388.315813
index 311.214286
syndrome 289.708333
factor 184.246753
loss 168.578313
index 451.123455
factor 321.676544
What i want to do is to read every line and print it. However if i encounter the same word again (which is in the above case, "index") i want to print the word as well as the max number that has been read. how can i do that? So far i ve written this piece of code.
word=''
cvalue=''
for line in open('example.txt','r'):
words=string.split(line)
wordcount=len(words)
if wordcount == 2:
word=words[0]
cvalue=words[1]
if word == words[0]:
max_cvalue=max(words[1],cvalue)
print word, max_cvalue
I know it is not much but i am a new user of python and i really like it :*
The thing i do not understand is how can i write in python "if you encounter the same word, then look at the value and print the word as well as the max value"
To be more specific from the above example here is what i would like to have as a result:
syndrome 289.708333
loss 168.578313
index 451.123455
factor 321.676544
Any help will be deeply appreciated.