Skip to content
Advertisement

How to get index of the max element from two lists after sorting it?

I have three lists price1, price2 and deviation. I wanted to find the top three highest price between price1 and price2. So to achieve that I first sorted them in decreasing order then I took the first 3 elements from both the list price1 and price2, and after that, I found the max between each max. Now I want to know the original position of the max value that is obtained after all this so that I can put it in deviation. Before sorting price1 and price2:

JavaScript

After sorting to get the top 3 prices:

JavaScript

output:

JavaScript

To get the max from it:

JavaScript

output:

JavaScript

Now What I want is that I want to find the postions of these max values. for example:

JavaScript

and ultimately in the end I want to put these positions into the deviation list to get the value in front of these prices. so:

JavaScript

Advertisement

Answer

No need to use list.index(), zip() is enough:

JavaScript

Prints:

JavaScript

EDIT: To have sublists in price1/price2/deviation lists, you can do:

JavaScript

Prints:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement