[英]How can I find all datetimes in a list within n minutes of a given value?
我有两个时间列表(Hour:Min:Sec 格式),我一直在努力将list_a
中的每个条目与所有list_b
进行比较,以确定 30 分钟内的值:
list_a = ["10:26:42", "8:55:43", "7:34:11"]
list_b = ["10:49:20", "8:51:10", "10:34:35", "8:39:47", "7:11:49", "7:42:10"]
预期 Output:
10:26:42 is within 30m of 10:49:20, 10:34:35
8:55:43 is within 30m of 8:51:10, 8:39:47
7:34:11 is within 30m of 7:11:49, 7:42:10
到目前为止,我一直在做的是:
import datetime
# Convert the Lists to Datetime Format
for data in list_a:
convert = datetime.datetime.strptime(data,"%H:%M:%S")
list_a_times.append(convert)
for data in list_b:
convert = datetime.datetime.strptime(data,"%H:%M:%S")
list_b_times.append(convert)
# Using a Value of List A, Find the Closest Value in List B
for data in list_a_times:
closest_to_data = min(list_b_times, key=lambda d: abs(d - data))
print(data, closest_to_data)
这种工作,但它只找到一个最接近的值? 我如何操纵 min() function 以保持提供值,只要它们在所需的 30 分钟或更短的时间内?
您在所有元素的绝对时间差异处循环和掠夺,而不是使用min
:
list_a = ["10:26:42", "8:55:43", "7:34:11"]
list_b = ["10:49:20", "8:51:10", "10:34:35", "8:39:47", "7:11:49", "7:42:10"]
import datetime
import datetime
# Convert the Lists to Datetime Format
list_a = [datetime.datetime.strptime(d,"%H:%M:%S") for d in list_a]
list_b = [datetime.datetime.strptime(d,"%H:%M:%S") for d in list_b]
for value in list_a:
for v in list_b:
if abs(value-v) < datetime.timedelta(minutes=30):
print (value, "=>", v, "diff: ", (value-v).total_seconds() // 60)
print()
Output:
1900-01-01 10:26:42 => 1900-01-01 10:49:20 diff: -23.0
1900-01-01 10:26:42 => 1900-01-01 10:34:35 diff: -8.0
1900-01-01 08:55:43 => 1900-01-01 08:51:10 diff: 4.0
1900-01-01 08:55:43 => 1900-01-01 08:39:47 diff: 15.0
1900-01-01 07:34:11 => 1900-01-01 07:11:49 diff: 22.0
1900-01-01 07:34:11 => 1900-01-01 07:42:10 diff: -8.0
对于像 0:05:00 和 23:55:00 这样的日期时间,这将 go 错误,因为它们位于不同的日期。
您可以使用自写的增量计算来解决此问题:
def abs_time_diff(dt1, dt2, *, ignore_date = False):
if not ignore_date:
return abs(dt1-dt2)
# use day before, this day and day after, report minimum
return min ( (abs(dt1 + datetime.timedelta(days = delta) - dt2)
for delta in range(-1,2)))
list_a = ["0:5:0"]
list_b = ["0:20:0", "23:55:0"]
list_a = [datetime.datetime.strptime(d,"%H:%M:%S") for d in list_a]
list_b = [datetime.datetime.strptime(d,"%H:%M:%S") for d in list_b]
for value in list_a:
for v in list_b:
print (value, v, abs_time_diff(value,v))
print (value, v, abs_time_diff(value,v, ignore_date = True))
Output:
1900-01-01 00:05:00 1900-01-01 00:20:00 0:15:00
1900-01-01 00:05:00 1900-01-01 00:20:00 0:15:00
1900-01-01 00:05:00 1900-01-01 23:55:00 23:50:00 # with date
1900-01-01 00:05:00 1900-01-01 23:55:00 0:10:00 # ignores date
IIUC,你想比较所有的组合,所以你需要检查所有。
请阅读答案的结尾以获取有关datetime
/ timedelta
的说明。
使用itertools.product
:
list_a = ['10:26:42', '8:55:43', '7:34:11']
list_b = ['10:49:20', '8:51:10', '10:34:35', '8:39:47', '7:11:49', '7:42:10']
import datetime
from itertools import product
str2time = lambda s: datetime.datetime.strptime(s, "%H:%M:%S")
for a,b in product(map(str2time, list_a), map(str2time, list_b)):
if abs(a-b).total_seconds() <= 1800:
print(f'{a:%H:%M:%S} is within 30m of {b:%H:%M:%S}')
output:
10:26:42 is within 30m of 10:49:20
10:26:42 is within 30m of 10:34:35
08:55:43 is within 30m of 08:51:10
08:55:43 is within 30m of 08:39:47
07:34:11 is within 30m of 07:11:49
07:34:11 is within 30m of 07:42:10
使用嵌套的 for 循环:
import datetime
str2time = lambda s: datetime.datetime.strptime(s, "%H:%M:%S")
for a in map(str2time, list_a):
start = f'{a:%H:%M:%S} is within 30m of'
for b in map(str2time, list_b):
if abs(a-b).total_seconds() <= 1800:
print(f'{start} {b:%H:%M:%S}', end='')
start = ','
if start == ',':
print()
output:
10:26:42 is within 30m of 10:49:20, 10:34:35
08:55:43 is within 30m of 08:51:10, 08:39:47
07:34:11 is within 30m of 07:11:49, 07:42:10
datetime
时间的注意事项使用不带日期的datetime
将默认为 1900-01-01,这可能会在接近午夜时产生边缘效应。 相反,您可以使用timedelta
对象。 使用我的代码,您需要将str2time
function 更改为:
def str2time(s):
h,m,s = map(int, s.split(':'))
return datetime.timedelta(hours=h, minutes=m, seconds
并稍微修改一下代码以便能够转换为字符串:
z = datetime.datetime(1900,1,1)
for a in map(str2time, list_a):
start = f'{z+a:%H:%M:%S} is within 30m of'
for b in map(str2time, list_b):
if abs(a-b).total_seconds() <= 1800:
print(f'{start} {z+b:%H:%M:%S}', end='')
start = ','
if start == ',':
print()
我将 go 提出使用pandas
的建议:
# Convert to pandas datetime series
import pandas as pd
dt_a = pd.Series(list_a, dtype='datetime64[ns]')
dt_b = pd.Series(list_b, dtype='datetime64[ns]')
# Comparison loop
interv_size = '30m' # Thirty minutes
for el in dt_a:
hits = df_b.loc[ abs(el - df_b) < interv_size ].dt.time
print(f'{el.time()} is within {interv_size} of', *hits)
优势? 你让 python 处理你的日期格式
from datetime import datetime, timedelta
list_a = ["10:26:42", "8:55:43", "7:34:11"]
list_b = ["10:49:20", "8:51:10", "10:34:35", "8:39:47", "7:11:49", "7:42:10"]
time_format = "%H:%M:%S"
def convert_to_datetime(time_str):
return datetime.strptime(time_str, time_format)
# Overriding list_a and list_ to avoid polluting the namespace
# Sorting for simple optimization
list_a = sorted([convert_to_datetime(time_str) for time_str in list_a])
list_b = sorted([convert_to_datetime(time_str) for time_str in list_b])
time_range_limit_in_seconds = timedelta(minutes=30).total_seconds()
result = []
for list_a_datetime in list_a:
with_in_time_limit = []
for list_b_datetime in list_b:
difference_in_seconds = (
list_a_datetime-list_b_datetime).total_seconds()
if difference_in_seconds <= time_range_limit_in_seconds:
# Convert back to string
with_in_time_limit.append(
list_b_datetime.strftime(time_format)
)
# Since the list is sorted, all the rest don't fall in time range
if difference_in_seconds < 0:
break
print(list_a_datetime.strftime(time_format), with_in_time_limit)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.