我想得到第一个值的最大值10,40->;40 我可以在下面使用它,但是有没有一个python方法代替for循环
soilLayer[0] = [ 10, 50, 0.3, 1600, 1800, 5, 30 ]
soilLayer[1] = [ 40, 50, 0.3, 1600, 1800, 5, 30 ]
heigth = []
position = 0
for name in sorted( soilLayer.keys() ):
heigth.append( soilLayer[name][position] )
print( max( heigth ) )
也可以采用非功能性解决方案:
这比@Kasramvd's functional solution效率低
如果您乐于使用第三方库,另一种方法是使用
numpy
:首先,当你想找到最大值时,你不需要排序。可以使用
operator.itemgetter
一次获取多个项目,使用zip获取第一个项目。然后,您可以对结果执行所有max
相关问题 更多 >
编程相关推荐