如何比较版本号python

时间:2019-03-25 15:58:27

标签: python

如果我有这个

a = "4.1.3.79"
b = "4.1.3.64"

如何进行比较以返回 True

a > b

我无法使用float()int()并且python无法将4.1.2.79识别为数字,我该如何与这些类型的值进行比较?

我已经读过StrictVersion,但只能读到第3个版本号,而不是4。

1 个答案:

答案 0 :(得分:1)

这将起作用:

a = "4.1.2.79"
b = "4.1.3.64"
#split it
a_list = a.split('.')
b_list = b.split('.')
#to look if a is bigger or not
a_bigger = False
#compare it number by number in list
for i in range(0, len(a_list)):
    #make from both lists one integer
    a_num = int(a_list[i])
    b_num = int(b_list[i])
    #if one num in the a_list is bigger
    if a_num > b_num:
        #make this variable True
        a_bigger = True
        #break the loop so it will show the results
    #else it wil look to the other nums in the lists
#print the result
print('a > b: %s' % str(a_bigger))