如何获取嵌套列表python中每一列的最大值?
How to get maximum values of each column in a nested list python?
假设我有一个嵌套的值列表
values=[[4,2,3],[16,5,0],[3,200,6],[0,10,12]]
max(values[x][0]) to get 16
max(values[x][1]) to get 200
max(values[x][2]) to get 12
我想要列表的输出
[16,200,12]
使用 zip
解包 values
转置矩阵并将 max
应用于新迭代器中的每个项目:
result = [max(x) for x in zip(*values)]
示例:
>>> values=[[4,2,3],[16,5,0],[3,200,6],[0,10,12]]
>>> result = [max(x) for x in zip(*values)]
>>> result
[16, 200, 12]
假设我有一个嵌套的值列表
values=[[4,2,3],[16,5,0],[3,200,6],[0,10,12]]
max(values[x][0]) to get 16
max(values[x][1]) to get 200
max(values[x][2]) to get 12
我想要列表的输出 [16,200,12]
使用 zip
解包 values
转置矩阵并将 max
应用于新迭代器中的每个项目:
result = [max(x) for x in zip(*values)]
示例:
>>> values=[[4,2,3],[16,5,0],[3,200,6],[0,10,12]]
>>> result = [max(x) for x in zip(*values)]
>>> result
[16, 200, 12]