1 回答
TA贡献1783条经验 获得超4个赞
result_list是一个平面列表,没有组,索引 0 处的值的 ,row[0]也是如此。8
将日期值正确转换为日期时间后,请使用各种方法(例如 ).hour来提取所需的值。
一种选择是使用列表理解对日期和相关评论计数进行分组。
from datetime import datetime
# here is your list of values
result_list = ['8/16/2016 9:55', '6.0', '11/22/2015 13:43', '29.0']
# set the format
date_format = '%m/%d/%Y %H:%M'
# group each date and count together
rl_updated1 = [result_list[k:k+2] for k in range(0, len(result_list), 2)]
print(rl_updated1)
[out]:
[['8/16/2016 9:55', '6.0'], ['11/22/2015 13:43', '29.0']]
# convert to a datetime format in a list comprehension
rl_updated2 = [[datetime.strptime(v[0], date_format), v[1]] for v in rl_updated1]
print(rl_updated2)
[out]:
[[datetime.datetime(2016, 8, 16, 9, 55), '6.0'],
[datetime.datetime(2015, 11, 22, 13, 43), '29.0']]
# alternatively, iterate through rl_updated1, before converting to a date format
for group in rl_updated1:
dt = group[0]
dt = datetime.striptime(dt, date_format)
保持平面列表,并从索引中选择日期,其中i % 2 == 0
for i, v in enumerate(result_list):
if i%2 == 0:
time_dt = datetime.strptime(v, date_format)
print(time_dt)
else:
comment = v
print(v)
[out]:
2016-08-16 09:55:00
6.0
2015-11-22 13:43:00
29.0
添加回答
举报