首先,不要命名您的列表列表,因为它会在下游代码中使python的列表数据结构无用.
带有注释的代码:
my_list=[[12,9,10,5],[3,7,18,6],[1,2,3,3],[4,5,6,2]]
def maxColumn(my_list):
m = len(my_list)
n = len(my_list[0])
list2 = [] # stores the column wise maximas
for col in range(n): # iterate over all columns
col_max = my_list[0][col] # assume the first element of the column(the top most) is the maximum
for row in range(1, m): # iterate over the column(top to down)
col_max = max(col_max, my_list[row][col])
list2.append(col_max)
return list2
print(maxColumn(my_list)) # prints [12, 9, 18, 6]
另外,尽管您特别提到了一个无numpy解决方案,但是在numpy中,它是如此简单:
list(np.max(np.array(my_list), axis=0))
就是说,将my_list转换为一个numpy数组,然后沿列查找最大值(axis = 0表示您在数组中从上到下移动).