How to compare two versions?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
def ver_cmp(v1, v2):
    lst = map(lambda x, y: ((0 if not x else x), (0 if not y else y)),
              v1.split('.'), v2.split('.'))

    for num1, num2 in lst:
        if num1 != num2:
            if num1 > num2:
                return (1, 'greater')
            else:
                return (2, 'less')
    return (0, 'equal')

    # Example:
    #
    # >>> print ver_cmp('1.6.2', '1.6')
    # >>> (1, 'greater')

More like this

  1. . by jeremydw 3 years, 8 months ago
  2. madslug by catellar 2 years, 7 months ago
  3. watermark by dingdongquan 3 years, 3 months ago
  4. convert youtube url for iframe, js api by ivanff 1 year ago
  5. default url routing and shortcut by vicalloy 4 years, 10 months ago

Comments

syntaxsugar (on November 25, 2013):

Why not use distutils module?

from distutils.version import StrictVersion
>>> StrictVersion('1.6.2') > StrictVersion('1.6')
True

#

(Forgotten your password?)