Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to merge two lists and get names of lists with the highest value for each index?

Tags:

python

list

numpy

I am trying to compare two lists of odds from two bookmakers. They look like this:

List1 = ['2.66', '3.79', '1.88', '1.61', '2.51', '1.29', '2.29', '2.56', '3.16', '2.05', '2.95', '2.64', '2.26', '3.17', '2.64', '2.25']
List2 = ['2.70', '4.40', '1.87', '1.56', '2.50', '1.26', '2.33', '2.60', '3.20', '2.04', '3.00', '2.65', '2.25', '3.20', '2.65', '2.22']

I need to merge them and get the highest odds. I already did this with numpy:

numpy.array([List1, List2]).astype(float).max(axis = 0)
FinalList = [2.7 4.4 1.88 1.61 2.51 1.29 2.33 2.6 3.2 2.05 3.2 2.65 2.26 3.2 2.65 2.25]

The problem is that I can't know to which list each index belongs to. In this example what I need to get is:

NamesLists = [List2, List2, List1, List1, List1, List1, List2, List2, List2, List1, List2, List2, List1,  List2, List2, List1]

But I really have no idea how to do this.

like image 938
AlexReed Avatar asked Dec 06 '25 07:12

AlexReed


2 Answers

You can combine argmax and take_along_axis:

import numpy

List1 = ['2.66', '3.79', '1.88', '1.61', '2.51', '1.29', '2.29', '2.56', '3.16', '2.05', '2.95', '2.64', '2.26', '3.17', '2.64', '2.25']
List2 = ['2.70', '4.40', '1.87', '1.56', '2.50', '1.26', '2.33', '2.60', '3.20', '2.04', '3.00', '2.65', '2.25', '3.20', '2.65', '2.22']

tmp = numpy.array([List1, List2]).astype(float)
idx = tmp.argmax(axis=0)

FinalList = numpy.take_along_axis(tmp, idx[None], axis=0)[0]
# or: FinalList = tmp[idx[None], numpy.arange(tmp.shape[1])][0]
# array([2.7 , 4.4 , 1.88, 1.61, 2.51, 1.29, 2.33, 2.6 , 3.2 , 2.05, 3.  ,
#        2.65, 2.26, 3.2 , 2.65, 2.25])

NamesLists = numpy.array(['List1', 'List2'])[idx]
# array(['List2', 'List2', 'List1', 'List1', 'List1', 'List1', 'List2',
#        'List2', 'List2', 'List1', 'List2', 'List2', 'List1', 'List2',
#        'List2', 'List1'], dtype='<U5')

Note that idx is of the form:

array([1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0])

which might be easier to use than ['List2', 'List2', 'List1', ...]

like image 138
mozway Avatar answered Dec 08 '25 20:12

mozway


in general, what you can do is use the argmax function:

max_indexes = numpy.array([List1, List2]).argmax(axis = 0)

This would return: array([1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0])

This would give you the index of the list with the higher value.

If you want to turn this into the names you can do so using this line:

numpy.array(['List1', 'List2'])[max_indexes]

like image 36
Sören Etler Avatar answered Dec 08 '25 22:12

Sören Etler