Assuming that I have a list of arrays in Python 3.2
, and I want to output an array that contains every array elements, together with their index position in the list, which have the highest second elements. How can I achieve this goal in the most scalable way (i.e., without having to use the nested for
-loop )?
Input
a = [[2,3], [1,4,5], [1,4,6,2], [3,3,5], [9,4]]
Expected Output
res = [[[1,4,5], 1], [[1, 4, 6,2], 2], [[9,4], 4]]
Can someone please help assist me on how to do this without using nested for
-loop?
CodePudding user response:
You could do:
b = max(a, key=lambda x:x[1])[1]
[[j, i] for i, j in enumerate(a) if j[1]==b]
Out[6]: [[[1, 4, 5], 1], [[1, 4, 6, 2], 2], [[9, 4], 4]]