By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,358 Members | 1,292 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,358 IT Pros & Developers. It's quick & easy.

Re: maximum value in a column of file

P: n/a
maurizio wrote:
i tryed to use the module max of numpy,
the problem is that i don't know how to put the column of the file in an
(i'm new in phyton).
anyway if you think there is a better way.....
What kind of file is it? Did you pick numpy because you want to do
matrix operations (beyond just finding a maximum value), or was it just
the first thing you stumbled upon when researching the problem?

A simple pattern for finding the maximum value in a file, using only
plain Python code, is:

max_value = ... some very small value ...
for line in file:
value = ... extract value from line ...
if value max_value:
max_value = value

If it's not obvious what "some very small value" is, given the range of
data you're working with, you can do

max_value = None
for line in file:
value = ... extract value from line ...
if max_value is None or value max_value:
max_value = value

instead (this leaves max_value set to None if the file is empty)

A more experienced Python hacker might write

def get_all_values(file):
for line in file:
value = ... extract value from line ...
yield value


max_value = max(get_all_values(file))

instead. But this still leaves us with the problem of extracting the
value. The best way to do that depends on the kind of files you're
working with; for fixed-format text files, you could use string slicing
and int/float for conversion (e.g. "value = float(line[10:20])", see the
tutorial for details); for other text formats, you could use
split/partition or regular expressions, or maybe an existing module
(such as "csv"); for binary formats, there's a large
number of existing tools.

And if you really want to use numpy for other reasons than just getting
a maximum value from a column, there's plenty of stuff in NumPy and
SciPy ( that might be useful.

So, in other words, I guess we still need more info.


Jul 23 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.