-3

I've a list of string which are numbers (float) from which I need to pick the max number in the list. Since these numbers represent software version of a particular component, 4.10 would be latest version and 4.1 would be the oldest. And so I am trying to find the latest version from this list.

version = ["4.1", "4.4", "4.5", "4.2",  "4.9", "4.6", "4.7", "4.3", "4.8", "4.10"] 

highest = max(map(float, version))

But since the float function here remove the trailing 0 for 4.10. The map output returns with 4.1's, so float is certainly not the correct way to retrieve the highest version.

["4.1", "4.2", "4.3", "4.4", "4.5", "4.6", "4.7", "4.8", "4.9", "4.1"] 

And so in above list "4.9" is returned as the highest version.

Can you please suggest on how to get the max float number from the list?

Genesis
  • 33
  • 5
  • 1
    You have a list of strings representing decimals, no floats. Do you want to use floats or is it required to have strings as input? – andrbrue Oct 03 '20 at 17:09
  • The max float number is 4.9, because 4.10=4.1. Does that help? – g_bor Oct 03 '20 at 17:10
  • 3
    Convert your strings to floats, and try sorting them again. – Robert Harvey Oct 03 '20 at 17:11
  • 2
    You have a list of strings, not floats. Use `version.sort(key=lambda x: float(x))` to sort them (in place) as though they were float values. – martineau Oct 03 '20 at 17:15
  • ...and afterwards, the largest value will be the string in `version[-1]` (the end of of the sorted list). – martineau Oct 03 '20 at 17:25
  • `highest = max(version, key=lambda v: tuple(map(int, v.split('.'))))`. – ekhumoro Oct 03 '20 at 17:32
  • 1
    Possible duplicate: [Sorting a list of dot-separated numbers, like software versions](https://stackoverflow.com/q/2574080/984421). – ekhumoro Oct 03 '20 at 17:36
  • sorry, I should have more clear, understand that its a list of string but I needed to find the max number from the list. Thanks ekhumoro and martineau. – Genesis Oct 03 '20 at 17:39

3 Answers3

2

You need to redefine the function you are sorting by, for example:

   In [4]: version = ["4.1", "4.4", "4.5", "4.2",  "4.9", "4.6", "4.7", "4.3", "4.8", "4.10"]   
   In [5]: sorted(version, key=lambda x: [int(x.split(".")[0]), int(x.split(".")[1])])                                                                    
   Out[5]: ['4.1', '4.2', '4.3', '4.4', '4.5', '4.6', '4.7', '4.8', '4.9', '4.10']
Akavall
  • 82,592
  • 51
  • 207
  • 251
1

The dot in version numbering is not a numerical decimal separator. Since the minor number 10 does not make a major 1.

IF all the majors are the same (i.e., all are 4.), an easy thing you can do to compare versions is to remove the . and then casting to the integer. Following @Akavall answer:

sorted(version, key=lambda x: [int(x.replace('.',''))])

If there is for example a version 3.10 this DOES NOT work anymore.

roschach
  • 8,390
  • 14
  • 74
  • 124
0

You case isn't about floats but this can be a solution:

version = ["4.1", "4.4", "4.5", "4.2",  "4.9", "4.6", "4.7", "4.3", "4.8", "4.10"] 
high_first = max([x.split(".")[0] for x in version])
highest = max([x.split(".")[1] for x in version If int(x.split(".")[0]) == high_first ])
Umutambyi Gad
  • 4,082
  • 3
  • 18
  • 39
adir abargil
  • 5,495
  • 3
  • 19
  • 29