Reputation: 8586
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
Upvotes: 0
Views: 2779
Reputation: 30258
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)
Upvotes: 2
Reputation: 309831
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).
Upvotes: 3