[Tutor] How to calculate high value from multiple lines for each column

Alan Gauld alan.gauld at btinternet.com
Wed Feb 17 05:22:03 EST 2016


On 16/02/16 22:28, Fosiul Alam wrote:
> Hi
> I am very new to python, basically , I want to get the Maximum value for
> each column
> 
>                   0.000           0.000           0.000        0
> (0.0%)           0.000           0.600
>                   0.000           3.000           6.000        1
> (0.0%)           0.300           0.000
>                   3.000           0.000           0.000        0
> (0.0%)           0.000           0.000
>                   5.000           0.000           0.000        1
...
> 
> So  maximum value for 1st column=5
>       maximum value for 2nd column = 7
>       maximum value for 3rd colun =6
>        .......................
> 
> How can I do this ?

The classical way to represent a table of data is using a list of lists

myData = [[],[],[],[]]  # a table with 4 columns

You can then read the data line by line and insert the values into your
lists.

for line in dataSource:
   fields = line.split()
   myData[0].append(fields[0])
   myData[1].append(fields[1])
   etc

Then at the end find the max() of each column

for col in myData:
    print "Max = " max(col)

There are more efficient ways to do it but that's probably the simplest.
You need to fill in quite a few blanks, such as how you read your data
source - is it a file or what?

Try it and come back if you hit problems.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list