0

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?

2
  • You can indent your code/file with 4 spaces to make it format better. I've done it for you. The line breaks are quite essential here.
    – Thomas
    Nov 4, 2009 at 8:29
  • 2
    In Python, arrays are only for binary data (usually numeric) where you need to control the memory layout. docs.python.org/library/array.html For your problem, you would normally use lists. You could use arrays for the numeric data after you parse out the commas, and do the calculations with the array but don't read the raw character data into an array. Nov 4, 2009 at 8:53

3 Answers 3

6

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
4

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.

0
3

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

:)

4
  • 1
    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... Nov 4, 2009 at 8:42
  • 3
    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
    Nov 4, 2009 at 8:57
  • 3
    This is sorting the numbers alphabetically rather than numerically. Maybe you mean numbers = map(int, tokens[1:]) Nov 4, 2009 at 9:07
  • @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. Nov 4, 2009 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.