Version Number Comparison Function

Bill Mill bill.mill at gmail.com
Fri Mar 25 11:01:46 EST 2005


On 25 Mar 2005 07:34:38 -0800, Keith <vetter at lincom-asg.com> wrote:
> Is there a function for comparing version numbers?
> 
> E.g.
> 
> 0.1.0 < 0.1.2
> 1.876b < 1.876c
> 3.2.2 < 3.4
> 

Not by default AFAIK. How about something like (untested):

def test_version(v1, v2):
    v1, v2 = v1.split('.'), v2.split('.')
    for x, y in zip(v1, v2):
        if x < y: return v1
        if y > x: return v2

It assumes that v1 and v2 have the same amount of '.'s and that all of
the version numbers are of the same length (i.e. 1.1000 would be <
1.999). How general do you need to be?

Peace
Bill Mill
bill.mill at gmail.com



More information about the Python-list mailing list