gpt4 book ai didi

python - 返回具有最大值的列表索引

转载 作者:行者123 更新时间:2023-12-01 05:49:39 28 4
gpt4 key购买 nike

def findMaxDiff(l):
'list(list(int)), returns index of the row with the maximum difference between elements and the value of the difference in that row'
return (max(max(a) - min(a) for a in l), l.index(max(l, key=max)))

虽然我已经让它返回最大数字的值,但我无法完全让它返回该列表的正确索引。在这种情况下效果很好:

>>> findMaxDiff([[12,3,50,17], [10,5,9,100,31], [5,3,1]])
(95, 1)

但在本例中并非如此。

>>> findMaxDiff([[0,10],[99,99]])
(10, 1)
>>> findMaxDiff([[1],[2],[3]])
(0, 2)

对于第一个,它应该返回 (10,0),第二个应该返回 (0,0)。我尝试过 key=sum 和 key=max,但两者都返回相同的结果。

最佳答案

这个怎么样:

def thing(list_):                                  
temp = enumerate(max(x) - min(x) for x in list_)
return max(x[::-1] for x in temp)

关于python - 返回具有最大值的列表索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14806459/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com