版本号对比 -- Python实现

发布时间:2019-05-28 22:03:46编辑:auto阅读(1800)

    相同位数版本号大小比较:

     1 def abc(str1, str2):
     2     if str1 == "" or str2 == "":
     3         print("输入包含空字符串,请重新输入")
     4         return ("输入包含空字符串,请重新输入")
     5     elif str1 == str2:
     6         print("2个版本号相同")
     7         return ("2个版本号相同")
     8     elif int(str1[0]) > int(str2[0]):
     9         print("版本1的版本号更大")
    10         return ("版本1的版本号更大")
    11     elif int(str1[0]) < int(str2[0]):
    12         print("版本2的版本号更大")
    13         return ("版本2的版本号更大")
    14     abc(str1[2:], str2[2:])
    15 
    16 
    17 abc("5.3.2", "5.1.9")

     

    不同位数版本号大小比较:

     1 def fun_version(v1,v2):
     2     l_1 = v1.split('.')
     3     print(l_1)
     4     l_2 = v2.split('.')
     5     print(l_2)
     6     c = 0
     7     while True:
     8         if c == len(l_1) and c == len(l_2):
     9             print(0)
    10             return 0
    11         if len(l_1) == c:
    12             l_1.append(0)
    13         if len(l_2) == c:
    14             l_2.append(0)
    15         if int(l_1[c]) > int(l_2[c]):
    16             print(1)
    17             return 1
    18         elif int(l_1[c]) < int(l_2[c]):
    19             print(-1)
    20             return -1
    21         c += 1
    22 
    23 fun_version("7.2.6", "8.4.9.3")

    ['7', '2', '6']
    ['8', '4', '9', '3']
    -1

     

关键字