【发布时间】:2021-05-24 17:24:08
【问题描述】:
这是我的清单:
matched_rows_2 =[
['1', '07-09-2020', '8:43:02', '100', 'TTF'],
['2', '07-09-2020', '8:43:02', '100', 'GGY'],
['3', '07-09-2020', '7:53:08', '120', 'HHJ'],
['4', '07-09-2020', '7:54:01', '160', 'JJH'],
['5', '07-09-2020', '8:30:00', '160', 'RRT'],
['6', '07-09-2020', '10:10:10', '160', 'PPO'],
['7', '07-09-2020', '11:12:11', '100', 'KKG'],
['8', '07-09-2020', '11:31:55', '160', 'PPO']]
我正在尝试执行以下操作:
- 对于每个车辆编号 (
index[3]),我正在尝试获取日期时间最接近chosen_datetime的列表。
我尝试了多种方法,但似乎还没有奏效。 下面是我的鳕鱼e:
chosen_datetime = datetime.fromisoformat("2020-07-09 08:43:55+00:00")
dts = [datetime.strptime(sub[1] + ' ' + sub[2], "%d-%m-%Y %H:%M:%S").replace(tzinfo=timezone.utc) for sub in matched_rows_2]
for x in matched_rows_2:
closest_to_chosen_datetime = min(dts, key=lambda d: max( d, chosen_datetime) - min(d, chosen_datetime))
if closest_to_chosen_datetime:
print(x)
这是我想要的输出:
['1', '07-09-2020', '8:43:02', '100', 'TTF'],
['2', '07-09-2020', '8:43:02', '100', 'GGY'],
['3', '07-09-2020', '7:53:08', '120', 'HHJ'],
['5', '07-09-2020', '8:30:00', '160', 'RRT'],
这是我当前的输出:
['1', '07-09-2020', '8:43:02', '100', 'TTF'],
['2', '07-09-2020', '8:43:02', '100', 'GGY'],
['3', '07-09-2020', '7:53:08', '120', 'HHJ'],
['4', '07-09-2020', '7:54:01', '160', 'JJH'],
['5', '07-09-2020', '8:30:00', '160', 'RRT'],
['6', '07-09-2020', '10:10:10', '160', 'PPO'],
['7', '07-09-2020', '11:12:11', '100', 'KKG'],
['8', '07-09-2020', '11:31:55', '160', 'PPO']]
我真的不知道发生了什么,出了什么问题。
【问题讨论】:
-
为什么是两行
100?或者车号是index[4]? -
@Epsi95,就是这样。这意味着车辆
100包含 2 个组件 -
你能用
pandas吗? -
不,我更喜欢在这个循环中进行。最终我想把它们全部放回一个新列表中
-
输入不清楚,为什么 100
kkg不存在?
标签: python list datetime for-loop min