Version number comparison in Python

后端 未结 17 2068
小蘑菇
小蘑菇 2020-11-27 10:32

I want to write a cmp-like function which compares two version numbers and returns -1, 0, or 1 based on their compared va

17条回答
  •  执念已碎
    2020-11-27 11:18

    The most difficult to read solution, but a one-liner nevertheless! and using iterators to be fast.

    next((c for c in imap(lambda x,y:cmp(int(x or 0),int(y or 0)),
                v1.split('.'),v2.split('.')) if c), 0)
    

    that is for Python2.6 and 3.+ btw, Python 2.5 and older need to catch the StopIteration.

提交回复
热议问题