python:点分隔版本比较

2024-10-02 18:26:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我在写一个脚本,在这里我需要比较binnaries的版本,比如我需要得到10.2.3大于10.1.30。如果我去掉点,我将得到1023和10130,这使我的比较颠倒过来。你知道吗

10.2.3 > 10.1.30  ==  1023 !> 10130 

到目前为止,我得出的唯一解决办法是:

1. do split(".") on version number 
2. for each elemet of list i got, check  if len() is eq 1 , add one zero from  the left.
3. glue all  elements together and get  int (10.1.30 will became 100130).
4. process second version number same way and compare both as regular ints.

这样:

10.2.3 > 10.1.30  ==  100203 > 100130 

这是比较版本的唯一方法还是其他方法?你知道吗


Tags: andof方法版本脚本numberforon
2条回答

您可以借用distutils及其继承者用来比较版本号的代码

from distutils.version import StrictVersion # Or LooseVersion, if you prefer

if StrictVersion('10.2.3') > StrictVersion('10.2'):
    print "10.2.3 is newer"

您可以尝试:

list(map(int, '10.2.3'.split('.'))) > list(map(int, '10.1.30'.split('.')))
Out[216]: True

相关问题 更多 >