返回最大值列表的索引

2024-09-19 23:35:09 发布

您现在位置:Python中文网/ 问答频道 /正文

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)))

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

^{pr2}$

但在这种情况下没有。在

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

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


Tags: ofthekeyinindexdefwith情况