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
:
price1 = [12,1,2,4,45,67,433,111,45,12] #Total 10 elements in all the lists
price2 = [23,12,233,11,54,232,323,12,42,4]
deviation = [23,45,56,67,78,786,45,34,2,1]
After sorting to get the top 3 prices:
print("Price1: ",sorted(price1, reverse=True))
print("Price2: ",sorted(price2, reverse=True))
output:
Price1: [433, 111, 67]
Price2: [323, 233, 232]
To get the max from it:
sortedPrice1 = sorted(price1, reverse=True)
sortedPrice2 = sorted(price2, reverse=True)
print("Price1: ", sortedPrice1[:3])
print("Price2: ", sortedPrice2[:3])
for i,j in zip(sortedPrice1[:3], sortedPrice2[:3]):
print("max: ", max(i, j))
output:
Price1: [433, 111, 67]
Price2: [323, 233, 232]
max: 433
max: 233
max: 232
Now What I want is that I want to find the postions of these max values. for example:
433 is in `price1` at 6th position
233 is in `price2` at 2nd position
232 is in `price2` at 5th position
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:
deviation[6] = 45
deviations[2] = 56
deviations[5] = 786
Advertisement
Answer
No need to use list.index()
, zip()
is enough:
price1 = [12,1,2,4,45,67,433,111,45,12] #Total 10 elements in all the lists
price2 = [23,12,233,11,54,232,323,12,42,4]
deviation = [23,45,56,67,78,786,45,34,2,1]
for a, b, _ in zip( sorted(zip(price1, deviation), key=lambda k: k[0], reverse=True),
sorted(zip(price2, deviation), key=lambda k: k[0], reverse=True), range(3) ): # range(3) because we want max 3 elements
print(max(a, b, key=lambda k: k[0]))
Prints:
(433, 45)
(233, 56)
(232, 786)
EDIT: To have sublists in price1
/price2
/deviation
lists, you can do:
price1 = [[12, 2, 3, 4],[1, 2, 5, 56],[12,34,45,3],[23,2,3,4],[1,6,55,34]]
price2 = [[1, 2, 3, 4],[1, 2, 3, 4],[1, 2, 3, 4],[1, 2, 3, 4],[1, 2, 3, 4]]
deviation = [[10, 20, 30, 40],[10, 20, 30, 40],[10, 20, 30, 40],[10, 20, 30, 40],[10, 20, 30, 40]]
for p1, p2, dev in zip(price1, price2, deviation):
print('price1=', p1)
print('price2=', p2)
print('dev=', dev)
for a, b, _ in zip( sorted(zip(p1, dev), key=lambda k: k[0], reverse=True),
sorted(zip(p2, dev), key=lambda k: k[0], reverse=True), range(3) ): # range(3) because we want max 3 elements
print(max(a, b, key=lambda k: k[0]))
print()
Prints:
price1= [12, 2, 3, 4]
price2= [1, 2, 3, 4]
dev= [10, 20, 30, 40]
(12, 10)
(4, 40)
(3, 30)
price1= [1, 2, 5, 56]
price2= [1, 2, 3, 4]
dev= [10, 20, 30, 40]
(56, 40)
(5, 30)
(2, 20)
price1= [12, 34, 45, 3]
price2= [1, 2, 3, 4]
dev= [10, 20, 30, 40]
(45, 30)
(34, 20)
(12, 10)
price1= [23, 2, 3, 4]
price2= [1, 2, 3, 4]
dev= [10, 20, 30, 40]
(23, 10)
(4, 40)
(3, 30)
price1= [1, 6, 55, 34]
price2= [1, 2, 3, 4]
dev= [10, 20, 30, 40]
(55, 30)
(34, 40)
(6, 20)