如何使用python计算列表列表中的时间间隔?

我是
Python的初学者,无法计算时间间隔.

我将首先解释数据结构. sg_status中每个列表中的第二个元素是代表交通信号灯某些状态的代码.代码“0”,“1”,“3”代表交通灯的最小绿色状态.我想计算最小绿色的持续时间.一个minimum_green持续时间的定义是minimum_green_status旁边的第一个其他状态的开始时间减去minimum_green_status的开始时间.然后,在列表中添加最小绿色时间.

我的解决方案

def get_minimum_green(sg_status):   
    minimum_green_list = [] 
    start_time_green = None 
    for i in range(len(sg_status)):
        if not(sg_status[i][1] == '3' or sg_status[i][1] == '1' or sg_status[i][1] == '0' and \
        (i != 0 and sg_status[i-1][1] != '3' and sg_status[i-1][1] != '1' and sg_status[i-1][1] != '0')) :
            continue
        else:
            start_time_green = sg_status[i][0]

            for j in range(i,len(sg_status)):
                if not(sg_status[j][1] != '3' and sg_status[j][1] != '1' and sg_status[j][1] != '0' and \
                (sg_status[j-1][1] == '3' or sg_status[j-1][1] == '1' or sg_status[j-1][1] == '0')):
                    continue
                else:
                    end_time_green = sg_status[j][0]
                    print "start: "+str(start_time_green)
                    print "end  :" + str(end_time_green)
                    minumum_green = end_time_green - start_time_green
                    minimum_green_list.append(minumum_green) 
                    break 

    return str(minimum_green_list)  

但结果显然是错误的,它打印的start_time_green比正确的打印更多.我怀疑过滤器

   if not(sg_status[i][1] == '3' or sg_status[i][1] == '1' or sg_status[i][1] == '0' and \
                (i != 0 and sg_status[i-1][1] != '3' and sg_status[i-1][1] != '1' and sg_status[i-1][1] != '0'))

或关于这个的逻辑是不正确的.

sg_status =[[[datetime.datetime(2015, 7, 8, 3, 0, 24, 302000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'], 
             [datetime.datetime(2015, 7, 8, 3, 0, 24, 903000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
             [datetime.datetime(2015, 7, 8, 3, 0, 27, 600000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
             [datetime.datetime(2015, 7, 8, 3, 0, 28, 201000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
             [datetime.datetime(2015, 7, 8, 3, 0, 37, 500000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
             [datetime.datetime(2015, 7, 8, 3, 0, 38, 101000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
             [datetime.datetime(2015, 7, 8, 3, 1, 40, 500000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
             [datetime.datetime(2015, 7, 8, 3, 1, 41, 1000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
             [datetime.datetime(2015, 7, 8, 3, 1, 50, 600000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
             [datetime.datetime(2015, 7, 8, 3, 1, 50, 701000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
             [datetime.datetime(2015, 7, 8, 3, 1, 51, 502000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
             [datetime.datetime(2015, 7, 8, 3, 1, 55, 700000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
             [datetime.datetime(2015, 7, 8, 3, 1, 55, 901000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
             [datetime.datetime(2015, 7, 8, 3, 1, 57, 702000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
             [datetime.datetime(2015, 7, 8, 3, 1, 58, 703000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '<'],
             [datetime.datetime(2015, 7, 8, 3, 2, 1, 700000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'A'],
             [datetime.datetime(2015, 7, 8, 3, 2, 2, 701000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'A'],
             [datetime.datetime(2015, 7, 8, 3, 2, 3, 702000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
             [datetime.datetime(2015, 7, 8, 3, 2, 7, 700000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
             [datetime.datetime(2015, 7, 8, 3, 2, 10, 701000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
             [datetime.datetime(2015, 7, 8, 3, 2, 11, 802000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
             [datetime.datetime(2015, 7, 8, 3, 2, 12, 700000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
             [datetime.datetime(2015, 7, 8, 3, 2, 12, 801000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
             [datetime.datetime(2015, 7, 8, 3, 2, 13, 702000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
             [datetime.datetime(2015, 7, 8, 3, 2, 14, 703000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
             [datetime.datetime(2015, 7, 8, 3, 2, 15, 704000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
             [datetime.datetime(2015, 7, 8, 3, 2, 16, 705000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
             [datetime.datetime(2015, 7, 8, 3, 2, 16, 8
def get_minimum_green(sg_status):   
    minimum_green_list = [] 
    start_time_green = None 
    for i in range(len(sg_status)):
        if not(sg_status[i][1] == '3' or sg_status[i][1] == '1' or sg_status[i][1] == '0' and \
        (i != 0 and sg_status[i-1][1] != '3' and sg_status[i-1][1] != '1' and sg_status[i-1][1] != '0')) :
            continue
        else:
            start_time_green = sg_status[i][0]

            for j in range(i,len(sg_status)):
                if not(sg_status[j][1] != '3' and sg_status[j][1] != '1' and sg_status[j][1] != '0' and \
                (sg_status[j-1][1] == '3' or sg_status[j-1][1] == '1' or sg_status[j-1][1] == '0')):
                    continue
                else:
                    end_time_green = sg_status[j][0]
                    print "start: "+str(start_time_green)
                    print "end  :" + str(end_time_green)
                    minumum_green = end_time_green - start_time_green
                    minimum_green_list.append(minumum_green) 
                    break 

    return str(minimum_green_list)  

, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
[datetime.datetime(2015, 7, 8, 3, 2, 18, 700000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
[datetime.datetime(2015, 7, 8, 3, 2, 20, 701000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
[datetime.datetime(2015, 7, 8, 3, 2, 21, 702000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
[datetime.datetime(2015, 7, 8, 3, 2, 22, 903000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
[datetime.datetime(2015, 7, 8, 3, 2, 23, 700000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
[datetime.datetime(2015, 7, 8, 3, 2, 24, 701000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
[datetime.datetime(2015, 7, 8, 3, 2, 25, 702000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
[datetime.datetime(2015, 7, 8, 3, 2, 26, 703000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
[datetime.datetime(2015, 7, 8, 3, 2, 26, 804000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '0'],
[datetime.datetime(2015, 7, 8, 3, 2, 27, 805000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '1'],
[datetime.datetime(2015, 7, 8, 3, 2, 30, 700000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '1'],
[datetime.datetime(2015, 7, 8, 3, 2, 32, 801000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 2, 35, 700000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 3, 51, 400000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 3, 52, 1000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 3, 58, 500000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 3, 59, 101000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 5, 17, 900000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 5, 18, 401000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 5, 24, 800000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 5, 25, 301000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 5, 25, 902000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 5, 26, 603000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 5, 28, 704000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 5, 29, 305000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 5, 35, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 5, 35, 601000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 12, 100000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 13, 1000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 15, 402000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 15, 903000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 17, 700000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 19, 1000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 19, 402000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 23, 800000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 24, 501000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 25, 802000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 26, 303000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 26, 704000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 27, 205000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 27, 9

def get_minimum_green(sg_status):   
    minimum_green_list = [] 
    start_time_green = None 
    for i in range(len(sg_status)):
        if not(sg_status[i][1] == '3' or sg_status[i][1] == '1' or sg_status[i][1] == '0' and \
        (i != 0 and sg_status[i-1][1] != '3' and sg_status[i-1][1] != '1' and sg_status[i-1][1] != '0')) :
            continue
        else:
            start_time_green = sg_status[i][0]

            for j in range(i,len(sg_status)):
                if not(sg_status[j][1] != '3' and sg_status[j][1] != '1' and sg_status[j][1] != '0' and \
                (sg_status[j-1][1] == '3' or sg_status[j-1][1] == '1' or sg_status[j-1][1] == '0')):
                    continue
                else:
                    end_time_green = sg_status[j][0]
                    print "start: "+str(start_time_green)
                    print "end  :" + str(end_time_green)
                    minumum_green = end_time_green - start_time_green
                    minimum_green_list.append(minumum_green) 
                    break 

    return str(minimum_green_list)  

, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 28, 500000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 30, 801000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 31, 302000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 33, 200000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 33, 901000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 37, 402000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 38, 100000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 49, 700000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 54, 700000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 56, 701000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '3'],
[datetime.datetime(2015, 7, 8, 3, 8, 57, 702000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), '<'],
[datetime.datetime(2015, 7, 8, 3, 9, 0, 700000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'A'],
[datetime.datetime(2015, 7, 8, 3, 9, 1, 701000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'A'],
[datetime.datetime(2015, 7, 8, 3, 9, 2, 702000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
[datetime.datetime(2015, 7, 8, 3, 9, 6, 700000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
[datetime.datetime(2015, 7, 8, 3, 9, 7, 301000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
[datetime.datetime(2015, 7, 8, 3, 9, 8, 402000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
[datetime.datetime(2015, 7, 8, 3, 9, 9, 203000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],
[datetime.datetime(2015, 7, 8, 3, 9, 9, 704000, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=180, name=None)), 'B'],]

最佳答案 这可以使用单个循环来完成.

>初始化一个空的green_time_list以存储绿色间隔的时间,green_on = False以表示绿色当前未开启且green_start时间为无
>如果绿色亮起并且当前状态不是’0′,’1′,’3’之一,那么这是第一个非绿色条目.将绿色设置为关闭并计算当前条目的green_start和timestamp之间的时间差.将时差添加到green_time_list
>否则,如果绿色关闭且当前状态为“0”,“1”,“3”之一,那么这是第一个“绿色”条目.将绿色设置为on并在green_start_time中保存时间戳

这当然假设您的数据按时间顺序排列

def find_green_times(sg_status):
    green_on = False
    green_list = []
    green_start = None
    for s in sg_status:
        if not green_on and s[1] in ("0","1","3"):
            # Green was not on till now and now  a green status is seen
            # Save timestamp as green_start time
            green_start = s[0]
            # Set green is on
            green_on = True
        elif green_on and s[1] not in ("0", "1", "3"):
            # Green was on and a non-green status is seen
            # Set green to off 
            green_on = False
            # Compute time difference between time when green was
            # started and this timestamp
            green_list.append(s[0] - green_start)
            # Optionally reset the green time
            green_start = None
    return green_list, green_on, green_start

然后,对于您的给定输入,它返回

>>> find_green_times(sg_status)
([datetime.timedelta(0, 94, 401000), datetime.timedelta(0, 390, 898000)], False, None)
点赞