为了账号安全,请及时绑定邮箱和手机立即绑定

如何查明字典中的时间是否有关联?

如何查明字典中的时间是否有关联?

慕码人8056858 2023-12-12 09:56:29
这是我的所有代码:shaped_timevalues = dict()from datetime import datetimefmt = '%H:%M:%S'newlist= list()for i,j in time_values.items():    print('SEGMENTD:',i)    for one in range(len(j)-1):        one_hour = datetime.strptime(str(j[one]), fmt).strftime("%H")        one_min = datetime.strptime(str(j[one]), fmt).strftime("%M")        other_hour = datetime.strptime(str(j[one + 1]), fmt).strftime("%H")        other_min = datetime.strptime(str(j[one + 1]), fmt).strftime("%M")        if one_hour== other_hour and int(one_min) +10>= int(other_min):            newlist.append(['%s:%s'%(one_hour,one_min),'%s:%s'%(other_hour,other_min)])            shaped_timevalues.setdefault(i, []).append(['%s:%s'%(one_hour,one_min),'%s:%s'%(other_hour,other_min)])        else:            shaped_timevalues.setdefault(i, []).append(['%s:%s' % (one_hour, one_min)])print(newlist)print(shaped_timevalues)它需要两个值,有时会重复。输出如下:Edit2:因此,我想创建一个新的字典,如果值(时间)在段中连接,例如在5874022、'00:03:00'、'00:08:00'和'00:14:00'时间中链接,因此它们必须是该字典中的列表。结果应该是这样的:time_values = {'5874022': ['00:03:00', '00:08:00', '00:14:00'],                           ['07:43:00'], ['09:33:00'], ['17:18:00'], ['23:23:00'], ...}
查看完整描述

1 回答

?
翻阅古今

TA贡献1780条经验 获得超5个赞

我使用你的代码作为基线,然后构建了以下内容:


shaped_timevalues = dict()

from datetime import datetime

fmt = '%H:%M:%S'


for i,j in time_values.items():

    newlist = list()

    sequence = [j[0]] #list with the 'linked times', with the first value already inserted

    for n in range(1, len(j)):


        time1 = datetime.strptime(j[n-1], fmt)

        time2 = datetime.strptime(j[n], fmt)

        minutes = (time2 - time1).total_seconds()/60 #how many minutes in difference


        if minutes == 5 or minutes == 6:

            sequence.append(j[n])

        else:

            newlist.append(sequence)

            sequence = [j[n]]


    if len(sequence) > 0:

        newlist.append(sequence)

    shaped_timevalues[i] = newlist


print(shaped_timevalues)

查看完整回答
反对 回复 2023-12-12
  • 1 回答
  • 0 关注
  • 41 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信