简体   繁体   English

元组的最大值

[英]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 参考maxoperator.itemgetter


†: Similar to how sort and sorted work, that's why the information in the Sorting HowTo might be relevant as well. †:类似于sortsorted工作,这就是为什么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.

相关问题 如何获取字典元组的最大值 - How to get the maximum value of a dictionary tuple 获取元组列表中元素元组的最大值 - Getting the maximum value of element tuple in list of tuples 查找元组中最小元素的最大值 - Find maximum value of minimum elements in tuple 如何在Python 2.7中从元组列表中找到最大值? - How to find maximum value from a tuple list in Python 2.7? 从元组列表中查找最大值的所有出现? - Find all occurences of maximum value from a list of tuple? 如何在字典中的列表中返回元组中的最大值? - How to return maximum of value in a tuple within a list within a dictionary? 在具有重复元素的元组列表中提取显示最大值的字符串 - Extract string showing maximum value in a list of tuple with duplicated elements 在元组中以固定元素为键的字典中找到最大值 - Find the maximum value in a dictionary with a fixed element in a tuple as a key 获取给定元组键的字典中的最大值,其中元组的元素可能位于不同的位置 - Get maximum value in dictionary for a given tuple key where elements of the tuple may be in different positions 如何排列元组列表,以便删除与其他元组相比最高值相关联的元组并返回最大元组 - How to arrange a list of tuples so that the tuple associated with the highest value compared to other tuple is removed and returns the maximum one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM