0

Im trying to find the max/min values of the first column in an array short of importing anything

max = 0
min = 100000

for row in _file_in.readlines():
    Data = row.split(',') # Load first column
    if (Data[0] > max):
        max = Data[0]
    if (Data[0] < min):
        min = Data[0]
print min, max

I initialize max to 0 and min to some arbitrarily large number. My resulting code is able to print out the max # but the minimum # is printing out 100000 which is incorrect

3
  • 2
    Can you also include the content of your file? Commented Oct 20, 2015 at 3:19
  • 2
    Data[0] is a string. You should convert that to number. Perhaps use int Commented Oct 20, 2015 at 3:20
  • You really shouldn't use min, max as variable names as they clash with Python's builtins. Commented Oct 20, 2015 at 3:49

2 Answers 2

3

It looks like your data is from a file and so you're comparing a string to a number. In python2.x, the result from the comparison is only guaranteed to be consistent (the spec doesn't tell us what the result actually is).

You'll want to convert your strings to some sort of numeric type... e.g. first_col = float(Data[0]) for the comparison.

for row in _file_in.readlines():
    Data = row.split(',') # Load first column
    first_col = float(Data[0])
    if (first_col > max):
        max = Data[0]
    if (first_col < min):
        min = Data[0]
    print min, max

Note, we can do a whole lot better using builtin functions and list-comprehensions:

first_col = [float(row.split(',')[0]) for row in _file_in]
print min(fist_col), max(first_col)

Notice that we didn't have to specify any artificial minima or maxima compared to your original solution, so this would work with input that consisted of entirely negative numbers (for example).

Sign up to request clarification or add additional context in comments.

4 Comments

Great! Is there a way to remove the initialize of min to 10000? It seems like bad code to do this
@Bijan -- Turns out I was just typing that out :-) Have a look.
@Bijan You can use float('inf') to initialize min, if you are not going to use the built-in min function.
Really should be elif to avoid the unnecessary comparison. min, max aren't ideal variable names.
2

An alternative, you could create a generator for your first_col and then just iterate over it. No need for any artificial initialisation of mn or mx. Is memory efficient and only iterates over the column once:

first_col = (int(row.split(',')[0]) for row in _file_in)
mn = mx = next(first_col, None)
for i in first_col:
    if i < mn:
        mn = i
    elif i > mx:
        mx = i
print mn, mx

For an empty file it will print (None, None)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.