0

I`m writing a script, where i need to compare versions of binnaries , so for example i need to get 10.2.3 greater than 10.1.30 . If i remove dots, i will get 1023 and 10130, which twists my comparison upside down.

10.2.3 > 10.1.30  ==  1023 !> 10130 

Till now, the only solution i came out is :

1. do split(".") on version number 
2. for each elemet of list i got, check  if len() is eq 1 , add one zero from  the left.
3. glue all  elements together and get  int (10.1.30 will became 100130).
4. process second version number same way and compare both as regular ints.

This way :

10.2.3 > 10.1.30  ==  100203 > 100130 

Is this the only way to compare versions or there are others?

Danylo Gurianov
  • 545
  • 2
  • 7
  • 21

2 Answers2

1

You can borrow the code that distutils and its successors use to compare version numbers

from distutils.version import StrictVersion # Or LooseVersion, if you prefer

if StrictVersion('10.2.3') > StrictVersion('10.2'):
    print "10.2.3 is newer"
chepner
  • 497,756
  • 71
  • 530
  • 681
0

You can try:

list(map(int, '10.2.3'.split('.'))) > list(map(int, '10.1.30'.split('.')))
Out[216]: True
Ankur Ankan
  • 2,953
  • 2
  • 23
  • 38