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

这是我的所有代码:


shaped_timevalues = dict()

from datetime import datetime

fmt = '%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)

它需要两个值,有时会重复。输出如下:

https://img1.sycdn.imooc.com/6577bdfc0001532708440033.jpg

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'], ...}


慕码人8056858
浏览 46回答 1
1回答

翻阅古今

我使用你的代码作为基线,然后构建了以下内容:shaped_timevalues = dict()from datetime import datetimefmt = '%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] = newlistprint(shaped_timevalues)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python