[英]Is there a global function in python to find the maximum of a 2d array based on a specified dimension?
例子:
input=[(10,1),(2,3),(6,2)]
output= 3
(因为第2维的最大值是3,所以返回3
)
您可以将max
与key
参数一起使用。
input=[(10,1),(2,3),(6,2)]
output=max(input,key=lambda x:x[1])[1]
# 3
您可以使用itemgetter
(如果您不喜欢使用lambda
)
from operator import itemgetter
output=max(input,key=itemgetter(1))[1]
# 3
您可以使用 numpy 来做到这一点
import numpy as np
a = np.array(input)
print(a[: , 1].max())
我不知道有没有这个功能。 但是看看这个:
my_items = [(10,1),(2,3),(6,2)]
my_items.sort(key=lambda e: e[0])
print (my_items[0])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.