Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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', ...]

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading