Home  >  Q&A  >  body text

python - version number comparison method optimization

Recently I wrote a small method for the app version update function. It doesn’t feel very beautiful. How do you compare version numbers?

Version number adaptation format: pure numbers separated by .

def version_cmp(client_version, last_version):
    """
    func of compare version number
    :param str client_version:
    :param str last_version:
    :return:
    """
    client_version_list = client_version.split(".")
    last_version_list = last_version.split(".")
    try:
        for i in range(0, len(last_version_list)):
            if int(last_version_list[i]) > int(client_version_list[i]):
                return True
     except IndexError, e:
        return False
    return False
阿神阿神2702 days ago661

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-05-27 17:41:41

    Your version number should only increase upwards, not decrease downwards. In fact, you only need to compare whether the values ​​are equal

    reply
    0
  • Cancelreply