I was wondering how can I find minimum and maximum values from a dataset, which is basically a text file. It has 50 rows, 50 columns.
I know I can set up a control loop (for loop to be specific) to have it read each row and column, and determine the min/max values. But, I'm not sure how to do that.
I think the rows and columns need to be converted to list first and then I need to use the split()
function. I tried setting something up as follows, but it doesn't seem to work:
for x in range(4,50): # using that range as an example
x.split()
max(4,50)
print x
New to Python. Please excuse my mistakes.
Will this work for you?
Try something like this:
Note that split() takes an optional argument if you want to split on something other than whitespace (commas for example).
If the file contains a regular (rectangular) matrix, and you know how many lines of header info it contains, then you can skip over the header info and use NumPy to do this particularly easily:
Hmmm...are you sure that homework doesn't apply here? ;) Regardless:
You need to not only split the input lines, you need to convert the text values into numbers. So assuming you've read the input line into in_line, you'd do something like this:
Once you have a list of rows, you need to get columns:
Then you can just iterate through each row and each column calling max():
Edit: Here's more complete code showing how to open a file, iterate through the lines of the file, close the file, and use the above hints:
Edit: For new-school goodness, don't use my old, risky file handling. Use the new, safe version:
Now you've got a lot of assurances that you don't accidentally do something bad like leave that file open, even if you throw an exception while reading it. Plus, you can entirely skip
in_file.close()
without feeling even a little guilty.