[英]Maximum value of a tuple
I've this tuple: 我有这个元组:
lpfData = ((0.0, 0.0), (0.100000001490116, 0.0879716649651527), ..., (1.41875004768372, 0.481221735477448),..., (45.1781234741211, 0.11620718985796))
and I want to find the maximum value of the second column. 我想找到第二列的最大值。 So I use:
所以我使用:
maxLPFt = max(lpfData)
maxLPF = maxLPFt[1]
But I get always the value of the second column coupled with the maximum value of the first column. 但我总是得到第二列的值加上第一列的最大值。 Basic stuff but google didn't help.
基本的东西,但谷歌没有帮助。
Cheers 干杯
Joao 若昂
You can pass a function as key
argument to extract the value you want to compare † : 您可以将函数作为
key
参数传递以提取要比较的值† :
import operator
maxLPFt = max(lpfData, key=operator.itemgetter(1))
This will use the second element of each tuple for the calculation. 这将使用每个元组的第二个元素进行计算。
Reference : max
, operator.itemgetter
参考 :
max
, operator.itemgetter
†: Similar to how sort
and sorted
work, that's why the information in the Sorting HowTo might be relevant as well. †:类似于
sort
和sorted
工作,这就是为什么Sorting HowTo中的信息也可能是相关的。
Just to tack onto Felix Kling's answer, which I can't thank you enough for, if you would like to single out a max number in the tuple, add ['index'] on to the end of Felix Kling's code. 只是为了解决Felix Kling的答案,我不能完全感谢你,如果你想在元组中挑出一个最大数字,在Felix Kling的代码末尾添加['index']。 For my application I needed the max number which resided in the second index of each tuple.
对于我的应用程序,我需要最大数字,它位于每个元组的第二个索引中。 My code was:
我的代码是:
maxLPFt = max(lpfData, key=operator.itemgetter(1))[1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.