我有兩個時間串列(小時:分鐘:秒格式),我一直在努力將每個條目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"]
預期輸出:
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)
這種作業,但它只找到一個最接近的值!只要值在所需的 30 分鐘或更短的時間內,我如何操縱 min() 函式以繼續提供值?
uj5u.com熱心網友回復:
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}')
輸出:
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()
輸出:
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函式更改為:
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()
uj5u.com熱心網友回復:
您在所有元素的絕對時間差異處回圈和掠奪,而不是使用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()
輸出:
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 這樣的日期時間會出錯,因為它們位于不同的日期。
您可以使用自寫的增量計算來解決此問題:
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))
輸出:
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
uj5u.com熱心網友回復:
我會提出一個建議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 處理你的日期格式
uj5u.com熱心網友回復:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/409552.html
標籤:
上一篇:C 11中的日期和時間表示類
