小编在这里总结下比较3位版本号的方法,比如1.2.1和1.3.1。相等就返回0,大于就返回1,小于就返回-1.
实现方法如下:

def compare_version(a, b):
    '''比较版本号'''
    la = a.split('.')
    lb = b.split('.')
    f = 0
    if len(la) > len(lb):
        f = len(la)
    else:
        f = len(lb)
    for i in range(f):
        try:
            if int(la[i]) > int(lb[i]):
                return 1
            elif int(la[i]) == int(lb[i]):
                continue
            else:
                return -1
        except IndexError as e:
            if len(la) > len(lb):
                return 1
            else:
                return -1
    return 0
Last modification:May 19, 2019
If you think my article is useful to you, please feel free to appreciate