从文件列表中查找最大版本

时间:2014-09-29 15:52:33

标签: python sorting max unique

请在我的Linux系统上获得一些python编码帮助。

我正在尝试对包含大约30个文件的目录进行排序。大多数文件都是重复的,但每次开发新代码时都会增加。我需要为每组文件选择最大版本。

我需要从下面显示的文件列表中获取AzMesa,AzChandler和AzPhoenix的最高版本。版本号始终跟在第二个“ - ”之后,并且在句号之前"。"。虽然有时城市发生变化,但这种格式不会改变,但它始终以Az开头,始终以rpm结束,有时候也会以#13; 13.13"我们发布代码时会增加。

AzMesa-13.13-1.x86_64.rpm
AzMesa-13.13-2.x86_64.rpm
AzMesa-13.13-3.x86_64.rpm
AzChander-13.13-1.x86_64.rpm
AzChander-13.13-2.x86_64.rpm
AzPhoenix-13.13-1.x86_64.rpm
AzPhoenix-13.13-2.x86_64.rpm
AzPhoenix-13.13-3.x86_64.rpm
AzPhoenix-13.13-4.x86_64.rpm
AzPhoenix-13.13-5.x86_64.rpm

下面的代码捕获所有以“Az”开头并以“rpm”结尾的文件。然后打印出包名称并打印出版本。

for name in glob.glob('Az*.rpm'):
  Package,Trash,CombinedVersion=name.split("-")
  print Package

  Version,Trash,Trash2=CombinedVersion.split(".")
  print Version

我需要一种方法,只从每个具有最高版本号的组中捕获一个文件,并将该输出发送到文件。

任何帮助将不胜感激。我并不是自称是蟒蛇开发者,只是尽我所能。

1 个答案:

答案 0 :(得分:1)

你可以使用python结构dict,它保存键值对,并将版本转换为int的元组,因此它具有可比性。

newest = dict()

for name in glob.glob('Az*.rpm'):
    #don't throw away the 13.13 - make one version
    package, combined_big_version, combined_version = name.split("-")

    #split the big vesrion into parts
    big1, big2 = combined_big_version.split(".")
    small_version, trash, trash2 = combined_version.split(".")

    #convert into a tuple of ints so we can compare them (biggest version first)
    #for example (13, 13, 1) < (13, 13, 2)
    #but         (13, 14, 1) > (13, 13, 4000)

    version = (int(big1), int(big2), int(small_version))


    #add to dictionary, or update if newer
    #store tuple (version, name) so we can get the name back 
    if not package in newest: 
        newest[package] = (version, name)
    else:
        newest[package] = max (newest[package], (version, name))

然后你可以这样做:

for package in newest.keys():
    print package, newest[package]

# AzMesa (13, 13, 3)
# AzPhoenix (13, 13, 5)
# AzChander (13, 13, 2)

print list(newest.items())

# [('AzMesa', (13, 13, 3)), ('AzPhoenix', (13, 13, 5)), ('AzChander', (13, 13, 2))]


# get the file names
names = [newest[key][1] for key in newest]
print (names)

# ['AzChander-13.13-2.x86_64.rpm', 'AzPhoenix-13.13-5.x86_64.rpm', 'AzMesa-13.13-3.x86_64.rpm']