6
def maxvalues():        
for n in range(1,15):
    dummy=[]
    for k in range(len(MotionsAndMoorings)):
        dummy.append(MotionsAndMoorings[k][n])
    max(dummy)
    L = [x + [max(dummy)]] ## to be corrected (adding columns with value max(dummy))
## suggest code to add new row to L and for next function call, it should save values here.

i have an array of size (k x n) and i need to pick the max values of the first column in that array. Please suggest if there is a simpler way other than what i tried? and my main aim is to append it to L in columns rather than rows. If i just append, it is adding values at the end. I would like to this to be done in columns for row 0 in L, because i'll call this function again and add a new row to L and do the same. Please suggest.

5
  • If you can use numpy the command is simply numpy.max(..). You can set which axis the max is taken over. Mar 13, 2016 at 5:47
  • @roadrunner66: what is the advantage of numpy if i use here? i'm bit new to python programming.? i would like to append the max(dummy) and add it to the column of row 0 of L. Could you please suggest what changes i should make in that program.? Thanks
    – Gopalpur
    Mar 13, 2016 at 5:53
  • @Vijaynitk Pls specify what exactly your input is and what your output should look like Mar 13, 2016 at 6:01
  • 2
    numpy is a very common numerical package, you can use a simple max function rather than write your own. Since it's written in C it is much faster than a Python loop for your own max. Mar 13, 2016 at 6:10
  • @schwobaseggl: I sorted out in sometime after i posted. I wished to see the different aspects to achieve the same. Here is what i coded and its running good. final_values=[] def maxvalues(): values=[Tp+1] for n in range(1,15): dummy=[] for k in range(len(MotionsAndMoorings)): dummy.append(MotionsAndMoorings[k][n]) if n>1 and n<7: val=abs(max(dummy)-min(dummy)) else: val=max(dummy) values.append(val) final_values.append(values)
    – Gopalpur
    Mar 13, 2016 at 8:35

2 Answers 2

6

General suggestions for your code

First of all it's not very handy to access globals in a function. It works but it's not considered good style. So instead of using:

def maxvalues():
    do_something_with(MotionsAndMoorings)

you should do it with an argument:

def maxvalues(array):
    do_something_with(array)

MotionsAndMoorings = something
maxvalues(MotionsAndMoorings) # pass it to the function.

The next strange this is you seem to exlude the first row of your array:

for n in range(1,15):

I think that's unintended. The first element of a list has the index 0 and not 1. So I guess you wanted to write:

for n in range(0,15):

or even better for arbitary lengths:

for n in range(len(array[0])): # I chose the first row length here not the number of columns

Alternatives to your iterations

But this would not be very intuitive because the max function already implements some very nice keyword (the key) so you don't need to iterate over the whole array:

import operator
column = 2
max(array, key=operator.itemgetter(column))[column]

this will return the row where the i-th element is maximal (you just define your wanted column as this element). But the maximum will return the whole row so you need to extract just the i-th element.

So to get a list of all your maximums for each column you could do:

[max(array, key=operator.itemgetter(column))[column] for column in range(len(array[0]))]

For your L I'm not sure what this is but for that you should probably also pass it as argument to the function:

def maxvalues(array, L): # another argument here

but since I don't know what x and L are supposed to be I'll not go further into that. But it looks like you want to make the columns of MotionsAndMoorings to rows and the rows to columns. If so you can just do it with:

dummy = [[MotionsAndMoorings[j][i] for j in range(len(MotionsAndMoorings))] for i in range(len(MotionsAndMoorings[0]))]

that's a list comprehension that converts a list like:

[[1, 2, 3], [4, 5, 6], [0, 2, 10], [0, 2, 10]]

to an "inverted" column/row list:

[[1, 4, 0, 0], [2, 5, 2, 2], [3, 6, 10, 10]]

Alternative packages

But like roadrunner66 already said sometimes it's easiest to use a library like numpy or pandas that already has very advanced and fast functions that do exactly what you want and are very easy to use.

For example you convert a python list to a numpy array simple by:

import numpy as np
Motions_numpy = np.array(MotionsAndMoorings)

you get the maximum of the columns by using:

maximums_columns = np.max(Motions_numpy, axis=0)

you don't even need to convert it to a np.array to use np.max or transpose it (make rows to columns and the colums to rows):

transposed = np.transpose(MotionsAndMoorings)

I hope this answer is not to unstructured. Some parts are suggestions to your function and some are alternatives. You should pick the parts that you need and if you have any trouble with it, just leave a comment or ask another question. :-)

0
4

An example with a random input array, showing that you can take the max in either axis easily with one command.

import numpy as np

aa= np.random.random([4,3]) 
print aa
print
print np.max(aa,axis=0)
print
print np.max(aa,axis=1)

Output:

[[ 0.51972266  0.35930957  0.60381998]
 [ 0.34577217  0.27908173  0.52146593]
 [ 0.12101346  0.52268843  0.41704152]
 [ 0.24181773  0.40747905  0.14980534]]

[ 0.51972266  0.52268843  0.60381998]

[ 0.60381998  0.52146593  0.52268843  0.40747905]
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.