时间戳匹配最近的情况

时间戳匹配

函数式编程-代码示例

for timestamp in timestamps:
closestTimestamp = min(index,key=lambda datetime : abs(timestamp - datetime))

def nearest(items, pivot):
return min(items, key=lambda x: abs(x - pivot))

def nearest(items, pivot):
return min([i for i in items if i <= pivot], key=lambda x: abs(x - pivot))

传统编程代码示例

-- coding: utf-8 --

def find_match_time(base_file, candidate_list,match_gap=1):
match_time = None
for sigle_file in candidate_list:
pose_timestamp = float(sigle_file)
timestamp_gap = abs(pose_timestamp - float(base_file))
if timestamp_gap <= match_gap:
match_time = sigle_file
match_gap = timestamp_gap
if not match_time:
raise Exception('time not match', base_file)
return match_time

if name == "main":
extra_list=[,"1659173362","1659173359","1659173360","1659173361","1659173362"]
choose_file="1659173360"
time_dat = find_match_time(choose_file, extra_list)
print(time_dat)

参考

Python - Locating the closest timestamp
https://stackoverflow.com/questions/32237862/find-the-closest-date-to-a-given-date

posted on 2022-08-30 17:48  辰令  阅读(321)  评论(0)    收藏  举报