简体   繁体   中英

Getting the index of the min item on a list

I want to get the index of the min item on a list, but in case of a tie. I'd like to use the position of both min items and compare those position in another list.

order = [4, 1 ,2 ,1 ]
LPT = [20, 10, 5, 20]
new_order = []

I want to get the index from the min values in the order list and in case of a tie, use the max value from the same position in LPT.

new_order should be like this:

new_order = [3,1,2,0]

Use a list-comprehension:

[x for x, _ in sorted(enumerate(zip(order, LPT)), key=lambda x: (x[1][0], -x[1][1]))]

Code :

order = [4, 1, 2, 1]
LPT = [20, 10, 5, 20]

new_order = [x for x, _ in sorted(enumerate(zip(order, LPT)), key=lambda x: (x[1][0], -x[1][1]))]
# [3, 1, 2, 0]

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