简体   繁体   中英

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 :

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

The list method index returns the position of an element in a list. If you replace your max function with an if statement, you can produce the desired output

for i,j in zip(sortedPrice1[:3], sortedPrice2[:3]):
    if i > j:
        print("%d is in `price1` at the %dth position" % (i, price1.index(i))
    else:
        print("%d is in `price2` at the %dth position" % (j, price2.index(j))

Note that this code:

  1. Doesn't deal with the case i=j
  2. Doesn't deal with multiple members of price1/price2 which are both in the top three.

You can do it with a try / except too but with the same limitations as @Yberman states in his answer but without needing to define any other variable than the lists itself:

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 i,j in zip(sorted(price1,reverse=True)[:3], sorted(price2,reverse=True)[:3]):
    try:
      print(deviation[price1.index(max(i, j))])
    except ValueError:
      print(deviation[price2.index(max(i,j))])

Output:

45
56
786

First create a copy of all 3 lists such as,

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]

d_p1=price1
d_p2=price2
d_dev=deviation

then perform all the operation to get max elements on this duplicate lists, and then when you have max elements in a list such as,

max_l=[433,233,232]

traverse through this list to get position,

for i  in max_l:
   if i in price_1:
       print("index:",price_1.index(i))
   else:
       print("index:",price_2.index(i))

if the positions of elements are changing after performing some operations then this process can be helpful to find original positions.

You can do it it two lines of code.

First find the index.

item_index_in_original_list = price1.index(433)

Then use it in your deviation list

deviation_of_max_item = deviation[item_index_in_original_list]

To repeat this for other items you can make a function with these two lines and call it.

def find_deviation(item_value,original_list):
    item_index_in_original_list = original_list.index(item_value)
    return deviation[item_index_in_original_list]

required_deviation = find_deviation(433, price1)

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)

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