0

我正在编写一个脚本,我需要比较二进制文件的版本,所以例如我需要让 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 

这是比较版本的唯一方法还是有其他方法?

4

2 回答 2

1

您可以借用代码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"
于 2013-11-07T15:49:46.910 回答
0

你可以试试:

list(map(int, '10.2.3'.split('.'))) > list(map(int, '10.1.30'.split('.')))
Out[216]: True
于 2013-11-07T15:52:21.517 回答