maximum value in a column of file

maurizio M.Busetto at isac.cnr.it
Wed Jul 23 12:20:06 CEST 2008


thank you for your answer
actually i've to do some statistics (maximum,minimum,mean,standard 
deviation,....) of a file of data in which each column is a particular 
type of data. (the file is a tab separated value).
I was trying to do this by using python (usually i work with fortran or 
bash, but i'm learning python), that the reason why i tried to use numpy.

Fredrik Lundh wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">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 array.
>> (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 (http://www.scipy.org/) that might be useful.
>
> So, in other words, I guess we still need more info.
>
> </F>
>
>
> </div>



More information about the Python-list mailing list