Python:唯一列表(基于部分文件路径)

2024-06-28 11:02:31 发布

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

对不起,我知道有一千个'使独特的名单'线程。我曾试图自己解决这个问题,或者尝试破解另一个“makeunique list”解决方案,但我的python技巧并不惊人,却没有成功。你知道吗

我有一个视频文件名列表(这些是电影中的快照)。对于任何给定的快照,我都希望根据路径的一部分(下图中用红色圆圈圈出)删除重复的快照;只有具有最高tk值的快照才会出现在最终列表中。你知道吗

例如在下图中,对于shotde05_001只有tk_3应该出现在列表中。

enter image description here

输入(有副本):

raw_list = ['D:\\de05\\de05_001\\postvis\\tk_2\\blasts\\tb205_de05_001.POSTVIS.mov', 
'D:\\de05\\de05_001\\postvis\\tk_3\\blasts\\tb205_de05_001.POSTVIS.mov', 
'D:\\de05\\de05_002\\postvis\\tk_1\\blasts\\tb205_de05_002.POSTVIS.mov', 
'D:\\de05\\de05_017\\postvis\\tk_2\\blasts\\tb205_de05_017.POSTVIS.mov', 
'D:\\de05\\de05_019\\postvis\\tk_2\\blasts\\tb205_de05_019.POSTVIS.mov', 
'D:\\de05\\de05_019\\postvis\\tk_3\\blasts\\tb205_de05_019.POSTVIS.mov', 
'D:\\de05\\de05_019\\postvis\\tk_4\\blasts\\tb205_de05_019.POSTVIS.mov', 
'D:\\de05\\de05_019\\postvis\\tk_1\\blasts\\tb205_de05_019.POSTVIS.mov', ]

输出(删除重复项,只保留最高的tk_uu编号):

outputList = ['D:\\de05\\de05_001\\postvis\\tk_3\\blasts\\tb205_de05_001.POSTVIS.mov', 
'D:\\de05\\de05_002\\postvis\\tk_1\\blasts\\tb205_de05_002.POSTVIS.mov', 
'D:\\de05\\de05_017\\postvis\\tk_2\\blasts\\tb205_de05_017.POSTVIS.mov', 
'D:\\de05\\de05_019\\postvis\\tk_4\\blasts\\tb205_de05_019.POSTVIS.mov', ]

任何帮助都会很好。非常感谢。你知道吗


Tags: 列表技巧解决方案线程快照tklistmov
1条回答
网友
1楼 · 发布于 2024-06-28 11:02:31

一种方法是创建一个字典并不断重新分配密钥,这样您就只能得到目录中的最后一个值:

import os

raw_list1 = [
    'D:\\\\de05\\de05_019\\postvis\\tk_2\\blasts\\tb205_de05_019.POSTVIS.mov',
    'D:\\\\de05\\de05_019\\postvis\\tk_3\\blasts\\tb205_de05_019.POSTVIS.mov',
    'D:\\\\de05\\de05_019\\postvis\\tk_4\\blasts\\tb205_de05_019.POSTVIS.mov',
    'D:\\\\de05\\de05_019\\postvis\\tk_1\\blasts\\tb205_de05_019.POSTVIS.mov',
    'D:\\\\tw05\\tw05_036\\postvis\\tk_9\\blasts\\tb205_tw05_036.POSTVIS.mov',
    'D:\\\\tw05\\tw05_036\\postvis\\tk_13\\blasts\\tb205_tw05_036.POSTVIS.mov'
]
raw_list2 = [
    'D:\\de05\\de05_001\\postvis\\tk_2\\blasts\\tb205_de05_001.POSTVIS.mov',
    'D:\\de05\\de05_001\\postvis\\tk_3\\blasts\\tb205_de05_001.POSTVIS.mov',
    'D:\\de05\\de05_002\\postvis\\tk_1\\blasts\\tb205_de05_002.POSTVIS.mov',
    'D:\\de05\\de05_017\\postvis\\tk_2\\blasts\\tb205_de05_017.POSTVIS.mov',
    'D:\\de05\\de05_019\\postvis\\tk_2\\blasts\\tb205_de05_019.POSTVIS.mov',
    'D:\\de05\\de05_019\\postvis\\tk_3\\blasts\\tb205_de05_019.POSTVIS.mov',
    'D:\\de05\\de05_019\\postvis\\tk_4\\blasts\\tb205_de05_019.POSTVIS.mov',
    'D:\\de05\\de05_019\\postvis\\tk_1\\blasts\\tb205_de05_019.POSTVIS.mov',
]

def path_split(p, folders=None):
    folders = folders or []
    head, tail = os.path.split(p)
    if not tail:
        return folders
    return path_split(head, [tail] + folders)

for raw_list in (raw_list1, raw_list2):
    results = {}

    for p in raw_list:
        # Split your path accordingly
        # For something simple you could have just done s.split('\\'), but since we're working with paths, we might as well use os.path.split
        shot1, shot2, folder1, take, folder2, file_name = path_split(p)
        # If something like 'de05_019' defines your shot, make that the key
        key = shot2
        # Extract the take number into an integer
        new_take_num = int(take.split('_')[-1])
        # Try finding the take you already stored (default to Nones)
        existing_take_num, existing_path = results.get(key, (None, None))
        # See if the new take is bigger than the existing one, based on the take number.
        # Lambda is there for comparison, meaning I'm only comparing the take numbers, not the paths. I'll link the docs to max in the comments.
        value = max((existing_take_num, existing_path), (new_take_num, p), key=lambda take_num_and_path: take_num_and_path[0])
        # Assign the value (which is either the existing take, or the new take)
        results[key] = value

    for res in sorted(results.values()):
        print res
    print '*' * 80

这将输出(您也可以print res[1]只打印路径):

(4, 'D:\\\\de05\\de05_019\\postvis\\tk_4\\blasts\\tb205_de05_019.POSTVIS.mov')
(13, 'D:\\\\tw05\\tw05_036\\postvis\\tk_13\\blasts\\tb205_tw05_036.POSTVIS.mov')
********************************************************************************
(1, 'D:\\de05\\de05_002\\postvis\\tk_1\\blasts\\tb205_de05_002.POSTVIS.mov')
(2, 'D:\\de05\\de05_017\\postvis\\tk_2\\blasts\\tb205_de05_017.POSTVIS.mov')
(3, 'D:\\de05\\de05_001\\postvis\\tk_3\\blasts\\tb205_de05_001.POSTVIS.mov')
(4, 'D:\\de05\\de05_019\\postvis\\tk_4\\blasts\\tb205_de05_019.POSTVIS.mov')
********************************************************************************

相关问题 更多 >