首页 > 解决方案 > 如何比较版本号python

问题描述

如果我有这个

a = "4.1.3.79"
b = "4.1.3.64"

我怎样才能进行这种比较以返回True

a > b

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

我已经阅读了,StrictVersion但这只会上升到第 3 个版本号,而不是 4。

标签: python

解决方案


这将起作用:

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))

推荐阅读