我正在尝试比较我的代码中的android版本号。如果任何版本小于4.1,我想要那个版本号。
是否应该直接对字符串使用比较,如下所示?
示例:
"4.0.3" < "4.1" # should return.
"5.0" < "4.1" # should not return.发布于 2017-07-21 07:31:45
尝尝这个
def compare_versions_greater_than(v1, v2):
for i, j in zip(map(int, v1.split(".")), map(int, v2.split("."))):
if i == j:
continue
return i > j
return len(v1.split(".")) > len(v2.split("."))
a = "2.0.3"
b = "2.1"
print(compare_versions_greater_than(a, b))
print(compare_versions_greater_than(b, a))输出
False
True发布于 2017-07-21 07:22:38
您可以将版本字符串转换为浮动。然后比较它们。
def version2float(version):
main, *tail = version.split('.')
temp = ''.join(tail)
return float('.'.join([main, temp]))https://stackoverflow.com/questions/45231304
复制相似问题