tags:

views:

216

answers:

3

I need to find out the maximum and minimum value in a line by reading a file and should be dividing the maximum value by the minimum value. Am interested to do this in python.

the contents of the file (file.txt) looks like this..

A28102_at,151,263,88,484,118,270,458,872,62,194
AB000114_at,72,21,20,61,20,85,20,25,20,65
AB000115_at,281,250,358,118,197,71,168,296,198,113

The problem am facing is i should be neglecting the first value, that is upto the first occurrence of comma and am unable to figure out a method. And also am interested to store the values in an array and then do the comparision. Is this approach correct or any better method is sugegsted?

+4  A: 

As you are a beginner, I won't give a copy/paste code snippet; there is more to learn if you figure the details out yourself. But here's what you could do:

  • read the file line-by-line in a loop, storing the current line in a string
  • for each line, split the string on commas, resulting in a list
  • drop the first element of the list
  • take the maximum of the rest

Maybe someone else will come up with the actual code.

Thomas
Thanks for the help thomas, I will be doing it the way you suggested!!!
Krishna Chaitanya
+3  A: 

I'm not sure if this is the most "pythonic" way of doing it, but it should work. I'm using lists instead of arrays.

for line in fp:
    tokens = line.split(',') # tokenize the line with comma as the only delimiter
    numbers = map(int, tokens[1:]) # skip the first value and convert to integer values
    maxValue = max(numbers) # max() operates on lists or sequences, I can't recall
    minValue = min(numbers) # so does min()
    print maxValue / minValue # TADA

:)

Michael Foukarakis
a more pythonic way may be to split and exclude in one line (`numbers=line.split(',')[1:]) and get directly the value (`print max(numbers)/min(numbers)`). min and max operates on an iterable: list, tuple, dict, iterators...
Adrien Plisson
Yeah, you're right. I broke it down to add the comments.
Michael Foukarakis
Many seem to think the pythonic way is putting everything on one single line. This is not true, don't forget that the idea behind Python is to keep a code which is clear and as readable as possible by others. `import this` will probably remind that in case of doubts ;-) Now there is the matter of code efficiency (using generators to avoid possibly very long lists, favouring list comprehensions over more costly filter/map constructions, ...) but that is something else.
RedGlyph
This is sorting the numbers alphabetically rather than numerically. Maybe you mean `numbers = map(int, tokens[1:])`
gnibbler
@gnibbler: thanks for pointing that out!
Michael Foukarakis
@RedGlyph: i don't think the pythonic way is to put everything on one line. i am a strong advocate of clode clarity, but here i don't see any benefit of using 2 temporary variables to hold the min and max when `min(numbers)/max(numbers)` is very explicit.
Adrien Plisson
+6  A: 

Python comes with batteries! Use the csv module to parse csv files:

#!/usr/bin/env python
import csv
csvobj=csv.reader(open('file.txt','r'))
for datum in csvobj:
    datum=[float(val) for val in datum[1:]] 
    print(datum)
    maximum=max(datum)
    minimum=min(datum)
    print(maximum/minimum)

# [151.0, 263.0, 88.0, 484.0, 118.0, 270.0, 458.0, 872.0, 62.0, 194.0]
# 14.064516129
# [72.0, 21.0, 20.0, 61.0, 20.0, 85.0, 20.0, 25.0, 20.0, 65.0]
# 4.25
# [281.0, 250.0, 358.0, 118.0, 197.0, 71.0, 168.0, 296.0, 198.0, 113.0]
# 5.04225352113
unutbu