List of maximum values of columns in a matrix without Numpy


#1

I’m trying to get a list of maximum values ​​of columns in a matrix without Numpy. Im trying to write tons of codes but cant find the wanted output.

Here is my code:

list=[[12,9,10,5],[3,7,18,6],[1,2,3,3],[4,5,6,2]]
list2=[]
def maxColumn(m, column):
  for row in range(len(m)):
    max(m[row][column]) # this didn't work

x=len(list)+1
for column in range(x):
  listt4.append(maxColumn(list, column))

print(list2)

And here is the wanted output:

[12,9,18,6]

#2

Hi @oganztrk2980237446,

You could write a function that computes the maximum values for each column and returns them in a list. You would then use it, as follows …

data = [[12,9,10,5],[3,7,18,6],[1,2,3,3],[4,5,6,2]]
print(max_in_each_column(data))

Output …

[12, 9, 18, 6]

Is there a reason why you chose, instead, to write a maxColumn function that computes the maximum for a single specified column at a time?

To write a function such as max_in_each_column, that computes the entire result, you could either use nested for loops that iterate through the columns and rows, respectively, or use a list comprehension that contains two for clauses. Give either strategy a try and post your code; we can help, if necessary.


#3

Did you mean to write list2?


#4

Yes, that appears to be a typographical error.

The function would need to accumulate the items from a column into a list, and then get the maximum value in that list. Another problem is that the function does not specify a return value.