版本號對比(Python)


相同位數版本號大小比較:

 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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM