简体   繁体   中英

Find all array with second highest elements in a list

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 (ie, 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM