0

这让我难住了...

我有一个文件夹中的文件列表。例如。

myFiles = ["apple_d_v01.jpg", "apple_d_v02.jpg", "apple_d_v03.jpg", "something_d.jpg", "anotherthing_d.jpg"]

文件“apple_d”有三个版本,版本后缀为“_vxx”。我希望能够将列表修改为只有最新版本,这样

myFiles = ["apple_d_v03.jpg", "something_d.jpg", "anotherthing_d.jpg"]

有任何想法吗?

非常感谢。

编辑:今天早上想出了-它可以正常工作,但与我最初提出的问题有点不同。谢谢大家帮忙。

myFiles = ["apple_d.jpg", "apple_dm.jpg", "apple_d_v2.jpg", "apple_d_v3.jpg", "something_d.jpg", "anotherthing_d.jpg", "test2_s_v01", "test2_s_v02.jpg", "test2_s_v03.jpg", "test2_s_v04.jpg" ]

objVersions = []


obj = "cube"  #controlled by variable
suf = "d"     #controlled by variable
ext = ".jpg"  #controlled by variable

for file in myFiles:


    if obj + "_" + suf + "_" in file:
        objVersions.append(file)

    if obj + "_" + suf + "." in file:
        objVersions.append(file)

objVersions = sorted(objVersions, reverse=True)

for file in objVersions:

    if ext not in file:
        objVersions.remove(file)


chosenfile = objVersions[0]
4

4 回答 4

0

假设这d是您问题中的版本号

latestVersion = max(int(fname.rsplit('.',1)[0].rsplit("_",1)[1].strip('v')) for fname in myFiles)

根据您的评论,我了解到您希望保留最新版本的版本化文件。为此,您将需要这个:

answer = set()
for fname in myFiles:
    name, version = fname.rsplit('.', 1)[0].rsplit("_",1)
    if version.startswith('v'): # this is a versioned file
        answer.add(
          max((fname for fname in myFiles if fname.startswith(name) and not fname.rsplit('.', 1)[0].endswith('d')), 
              key=lambda fname: int(
                     fname.rsplit('.', 1)[0].rsplit("_",1)[1].strip('v')) ))
    else:
        answer.add(fname)
于 2013-07-31T21:14:53.897 回答
0

我认为这个方法会按照你的要求做,它需要一个文件名列表并找到最新版本,然后搜索包含版本标签的所有文件并删除不是最新的文件。如果某些文件仅更新到版本 2 而其他文件仅更新到 3,它将无法正常工作。

def removePreviousVersions(FileNameList):
    returnList = []
    LatestVersion = 0
    for FileName in FileNameList:
        if FileName.find('_v') > -1:
            Name, Version = (FileName.replace('.jpg', '')).split('_v')
            if LatestVersion <  int(Version):
                LatestVersion = int(Version)

    argument = '_v'+ str(LatestVersion).zfill(2)
    for FileName in FileNameList:
        if FileName.find('_v') == -1:
            returnList.append(FileName)        
        elif FileName.find(argument) != -1:
            returnList.append(FileName)

    return returnList

此处的示例使用类似的方法工作,但它会抓取文件的最新版本,即使它是文件的最新版本的旧版本。

def removePreviousVersions(FileNameList):
TempFileNameList = []
ReturnList = []  
for FileName in FileNameList:
    if '_v' in FileName:
        Name, Version = (FileName.replace('.jpg', '')).split('_v')
        if Name not in TempFileNameList:
            TempFileNameList.append(Name)
            latestVersion = 0
            TempFileName = ''
            for fname in FileNameList:
                if Name in fname:
                    tName, tVersion = (fname.replace('.jpg', '')).split('_v')
                    if int(tVersion) > int(latestVersion):
                        latestVersion = int(tVersion)
                        TempFileName = fname
            ReturnList.append(TempFileName)
    else:
        ReturnList.append(FileName)

return ReturnList

示例输入:

名称列表 = [“stupd_d_v01.jpg”、“apple_d_v01.jpg”、“apple_d_v02.jpg”、“apple_d_v03.jpg”、“something_d.jpg”、“anotherthing_d.jpg”]

返回 ["stupd_d_v01.jpg", "apple_d_v03.jpg", "something_d.jpg", "anotherthing_d.jpg"]

于 2013-07-31T22:20:25.427 回答
0

假设版本始终使用 _v## 的语法,您可以这样做:

import re

parts_re = re.compile(r'^(.+_d)(.*)\.jpg$')

def remove_oldies(list):
    final = []
    saved_version = ''
    saved_name = ''
    for item in sorted(list):
        name, version = parts_re.search(item).group(1,2)
        if name != saved_name:
            if saved_name != '':
                final.append(saved_name + saved_version + ".jpg")
            saved_version = version
            saved_name = name
        else:
            saved_version = version
    final.append(saved_name + saved_version + ".jpg")
    return final

    remove_oldies(myFiles)
于 2013-08-01T02:27:58.270 回答
0

一个简短(且实用)的答案将类似于:

files= [ (f.split("_d")[0],int("0"+re.search("((_v)?([0-9]+|))\.jpg",f.split("_d")[1]).group(3)),f) for f in myFiles]
result= [ [ f[2] for f in files if f[0] == fn and f[1] == max( [ f[1] for f in files if f[0] == fn ] ) ][0] for fn in set( f[0] for f in files ) ]


添加了一些扩展和评论:

# List of tuples of the form ('apple', 2, 'apple_d_v02.jpg') and ('something', 0, 'something_d.jpg')
files= [ (f.split("_d")[0],int("0"+re.search("((_v)?([0-9]+|))\.jpg",f.split("_d")[1]).group(3)),f) for f in myFiles]
basename= 0  # index of basename (apple, something, etc) in each tuple inside "files"
version= 1  # index of version in each tuple inside "files"
fullname= 2  # index of full filename in each tuple inside "files"

result= [ [ f[fullname] for f in files if f[basename] == current_basename and f[version] == max( [ f[version] for f in files if f[basename] == current_basename ] ) ][0] for current_basename in set( f[basename] for f in files ) ]


最后一行可以进一步扩展为:

def max_version_fullname(current_basename):
    versions= [ f[version] for f in files if f[basename] == current_basename ]
    max_version= max( versions )
    fullnames_for_max_version= [ f[fullname] for f in files if f[basename] == current_basename and f[version] == max_version ]
    fullname_for_max_version= fullnames_for_max_version[0]
    return fullname_for_max_version
basenames= set( f[basename] for f in files )
result= [ max_version_fullname(current_basename) for current_basename in basenames ]
于 2013-08-01T03:12:07.127 回答