iKyriaki
iKyriaki

Reputation: 609

Returning index of list with greatest value

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

While I've gotten it to return the value of the maximum number, I can't quite get it to return the correct index of that list. It works fine in this case:

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

But doesn't in this case.

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

For the first one it should return (10,0), and the second should return (0,0). I've tried key=sum and key=max, but both return the same thing.

Upvotes: 1

Views: 175

Answers (1)

wim
wim

Reputation: 362557

How's this:

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

Upvotes: 2

Related Questions