【发布时间】:2019-04-03 22:05:35
【问题描述】:
我有两个数据源,每个都有一个文件列表。要求是比较两个列表并找出具有相同时间戳的文件,然后根据输入参数输出我想查看哪个源的结果列表。
一个复杂性是我们无法进行直接的文件名比较,因为它们是用关于源的字符串包装的,我们需要先提取日期部分并仅比较它,然后返回原始文件名。
没有大量使用 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']))
这段代码只是返回两个来源的文件之间的共同日期列表。
输出是:
['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"]
【问题讨论】:
标签: python