我是Python新手。手头的任务是在特定目录中的构建列表中查找最新版本。
我的代码如下所示:
command = 'ls ' + self.build_path
stdin, stdout, stderr = self.ssh.exec_command(command)
builds_list = stdout.read()
arrBuild = builds_list.split('\n')
joined = "|".join(arrBuild)
self.latest_packages['relayws'] = max(re.findall("mybuild-relayws_[0-9]+.[0-9].[0-9]+-[0-9]_amd64.deb", joined))
现在我的问题是,当我在列表中有“mybuild-relayws_44.0.101-1_amd64.deb”时,“mybuild-relayws_44.0.99-1_amd64.deb”始终被认为是最新的。
那是:
max(['mybuild-relayws_44.0.99-1_amd64.deb', 'mybuild-relayws_44.0.101-1_amd64.deb'])
始终返回mybuild-relayws_44.0.99-1_amd64.deb.
我知道max()在这里使用了字符串比较,从这个意义上说它返回正确。但是,就我而言,我期待mybuild-relayws_44.0.101-1_amd64.deb
我的问题是:
先谢谢。
答案 0 :(得分:3)
您尚未就如何预期构建订单提供任何规则。我假设你希望根据以下内容订购它们:
firebet-relayws_44.0.99-1_amd64.deb
=> 44.0.99-1
44.0.99-1
=>一系列数字(44, 0, 99, 1)
然后尝试类似的事情:
def get_version(txt):
# extract eg 44.0.99-1
version_data = txt.split("_")[1]
# split into 44.0.99 and 1
version, release = version_data.split("-")
# convert them to a list of numbers
return [int(x) for x in version.split(".")] + [int(release)]
然后,例如:
>>> print(get_version("firebet-relayws_44.0.99-1_amd64.deb"))
[44, 0, 99, 1]
现在我们将此作为max
>>> print(max(['mybuild-relayws_44.0.99-1_amd64.deb', 'mybuild-relayws_44.0.101-1_amd64.deb'], key=get_version))
mybuild-relayws_44.0.101-1_amd64.deb
答案 1 :(得分:1)
您需要从relayws
获取版本:
def version(relayws):
return map(int, relayws.split('_')[1].replace('-', '.').split('.'))
然后你可以通过键找到最大值:
latest = max(builds, key=version)