python输出list最大值_python-矩阵中的列的最大值列表(无Numpy)

首先,不要命名您的列表列表,因为它会在下游代码中使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表示您在数组中从上到下移动).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值