慕慕森
您可以通过传递表达式将列表理解与min方法结合使用。lambdafrom datetime import datetimel1 = [ '09/12/2017', '10/24/2017' ]l2 = [ '09/15/2017', '10/26/2017', '12/22/2017' ]l1 = [min(l2, key=lambda d: abs(datetime.strptime(d, "%m/%d/%Y") - datetime.strptime(item, "%m/%d/%Y"))) for item in l1]输出['09/15/2017', '10/26/2017']如果您想要更有效的解决方案,您可以编写自己的insert排序算法。def insertSortIndexItem(lst, item_to_insert): index = 0 while index < len(lst) and item_to_insert > lst[index]: index = index + 1 return lst[index]l2 = sorted(l2, key=lambda d: datetime.strptime(d, "%m/%d/%Y"))l1 = [insertSortIndexItem(l2, item) for item in l1]
狐的传说
如果您的列表很长,则值得进行预处理l2,以便能够使用它bisect来查找最近的日期。然后,找到最接近日期的日期l1将是 O(log(len(l2)) 而不是 O(len(l2)) min。from datetime import datetimefrom bisect import bisectl1 = [ '09/12/2017', '10/24/2017' ]l2 = [ '09/15/2017', '10/26/2017', '12/22/2017' ]dates = sorted(map(lambda d: datetime.strptime(d, '%m/%d/%Y'), l2))middle_dates = [dates[i] + (dates[i+1]-dates[i])/2 for i in range(len(dates)-1)]out = [l2[bisect(middle_dates, datetime.strptime(d,'%m/%d/%Y'))] for d in l1]print(out)# ['09/15/2017', '10/26/2017']为了解决您的最后一条评论,这是使用迭代器和生成器的另一种解决方案,它l1仅在 开始的必要部分结束l2:from datetime import datetimefrom itertools import tee, islice, zip_longestdef closest_dates(l1, l2): """ For each date in l1, finds the closest date in l2, assuming the lists are already sorted. """ dates1 = (datetime.strptime(d, '%m/%d/%Y') for d in l1) dates2 = (datetime.strptime(d, '%m/%d/%Y') for d in l2) dinf, dsup = tee(dates2) enum_middles = enumerate(d1 + (d2-d1)/2 for d1, d2 in zip_longest(dinf, islice(dsup, 1, None), fillvalue=datetime.max)) out = [] index, middle = next(enum_middles) for d in dates1: while d > middle: index, middle = next(enum_middles) out.append(l2[index]) return out一些测试:l1 = [ '09/12/2017', '10/24/2017', '12/11/2017', '01/04/2018' ]l2 = [ '09/15/2017', '10/26/2017', '12/22/2017' ]print(closest_dates(l1, l2))# ['09/15/2017', '10/26/2017', '12/22/2017', '12/22/2017']l2 = ['11/11/2018'] # only one date, it's always the closestprint(closest_dates(l1, l2))# ['11/11/2018', '11/11/2018', '11/11/2018', '11/11/2018']