gpt4 book ai didi

Python:给定两个文件名列表,根据日期部分查找常见文件名

转载 作者:行者123 更新时间:2023-12-01 08:06:40 25 4
gpt4 key购买 nike

我有两个数据源,每个数据源都有一个文件列表。要求是比较两个列表并找出具有相同时间戳的文件,然后根据有关我想查看哪个源的输入参数输出结果列表。

一个复杂之处是我们无法进行直接的文件名比较,因为它们被有关源的字符串包裹,我们需要首先提取日期部分并仅进行比较,然后返回原始文件名。

如果没有大量 Python 工作知识,我觉得我所尝试的方法不够有效。例如,我能够从两个源中提取日期部分并将它们与一段代码进行比较,但随后不确定如何将它们恢复到原始文件名。

listA = ["apple://folderx/foldery/sourcea_20190326-0.json", "apple://folderx/foldery/sourcea_20190323-1.json", "apple://folderx/foldery/sourcea_20190324-1.json"]
listB = ["apple://folderx/folderz/source_b_20190324-0.json", "apple://folderx/folderz/source_b_20190326-0.json"]

mySource = ['A', 'B']
allDates = {}
for s in mySource:
fileList = []
dateList = []

if s == 'A':
fileList = listA
elif s == 'B':
fileList = listB
for f in fileList:
date = f.rsplit('_',1)[-1].split('-')[0]
if not date in dateList:
dateList.append(date)

if len(dateList) > 0:
allDates[s] = dateList

else:
time.sleep(10)
if len(fileList) == 0:
raise NoDataException

list(set(allDates['A']).intersection(allDates['B']))

这段代码只是返回两个源文件之间的公共(public)日期列表。

输出为:

['20190326', '20190324'].

我正在寻找的是

listA = ["apple://folderx/foldery/sourcea_20190326-0.json", "apple://folderx/foldery/sourcea_20190324-1.json"] 
listB = ["apple://folderx/folderz/source_b_20190324-0.json", "apple://folderx/folderz/source_b_20190326-0.json"]

最佳答案

您可以为 listAlistB 构建字典,将日期映射到文件名,使用集合交集获取公共(public)键(日期),然后将日期映射回listAlistB 的文件名:

a, b = {}, {}
for l, d in (listA, a), (listB, b):
for f in l:
d.setdefault(f.rsplit('_', 1)[-1].split('-')[0], []).append(f)
common_keys = a.keys() & b.keys()
listA, listB = ([f for k in common_keys for f in d[k]] for d in (a, b))

因此给定:

listA = ["apple://folderx/foldery/sourcea_20190326-0.json", "apple://folderx/foldery/sourcea_20190323-1.json", "apple://folderx/foldery/sourcea_20190324-1.json"]
listB = ["apple://folderx/folderz/source_b_20190324-0.json", "apple://folderx/folderz/source_b_20190326-0.json", "apple://folderx/folderz/source_c_20190324-0.json"]

listA 变为:

['apple://folderx/foldery/sourcea_20190324-1.json', 'apple://folderx/foldery/sourcea_20190326-0.json']

listB 变为:

['apple://folderx/folderz/source_b_20190324-0.json', 'apple://folderx/folderz/source_c_20190324-0.json', 'apple://folderx/folderz/source_b_20190326-0.json']

关于Python:给定两个文件名列表,根据日期部分查找常见文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55505021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com