How to find the greatest values in a list and the corresponding values to that in another list?

83 Views Asked by At

I have two lists that have the same amount of elements. Each element is a number (float). I need to find the 10 greatest values in the first list, then find those values in the second list that have the same indices as the indices of the 10 greatest values in the first list. How can I do that? (I want to use numpy if possible). For instance:

a= [0.5, 2.9, 9.7]
b= [1, 3, 5.8]

I need the 2 greatest values from list a, which are 2.9 and 9.7. Their indices are 1 and 2, and so I need 3 and 5.8 from list b.

I haven't tried anything yet, I thought about amax or something, but I don't know.

2

There are 2 best solutions below

2
mozway On

Assuming this example (integers here for clarity, but this works identically with floats)

a1 = np.array([11,  6, 13,  8,  9,  5,  7, 10,  3, 14, 12,  4,  2,  1,  0])
# top 10:       x   x   x   x   x   x   x   x       x   x
a2 = np.array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])

You can find the indices of the top 10 values with numpy.argpartition, then index your second array:

out = a2[np.argpartition(a1, -10)[-10:]]
# array([ 5,  1,  6,  3,  4,  7, 10,  9,  2,  0])

If you want them in order:

out = a2[np.sort(np.argpartition(a1, -10)[-10:])]
# array([ 0,  1,  2,  3,  4,  5,  6,  7,  9, 10])

updated example:

a = np.array([0.5, 2.9, 9.7])
b = np.array([1, 3, 5.8])

out = b[np.sort(np.argpartition(a, -2)[-2:])]
# array([3. , 5.8])

pure python approach

a = [0.5, 2.9, 9.7]
b = [1, 3, 5.8]

N = 2
Nth = sorted(a)[-N]
out = [val_b for val_a, val_b in zip(a, b) if val_a >= Nth]
# [3, 5.8]
0
SIGHUP On

My interpretation of the requirement may not be correct. However, without additional modules I would do this:

a = [11, 6, 13, 8, 9, 5, 7, 10, 3, 14, 12, 4, 2, 1, 0]
b = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

a1 = [0.5, 2.9, 9.7]
b1 = [1, 3, 5.8]

def process(a, b, t):
    s = sorted([(v, i) for i, v in enumerate(a)])
    return [b[i] for _, i in s[-t:]]

print(process(a, b, 10))
print(process(a1, b1, 2))

Output:

[5, 1, 6, 3, 4, 7, 0, 10, 2, 9]
[3, 5.8]

Explanation:

Build a list of 2-tuples (value, index). Natural sort will be based on the values (first part of the tuple). Build output list using the indexes from the previously sorted list.

The output here differs from the answer provided using numpy but that doesn't seem to be a functional issue more of an understanding of the requirement