How to calculate time span in a list of lists using python?

I start with Python and cannot calculate time intervals.

First I will explain the data structure. The second element in each list in sg_status is the code that stands for a certain state of the traffic light. Code "0", "1", "3" represents the minimum green state of the traffic light. I want to calculate the duration of the minimum green. Determining one minimum duration is that the start time of the first other state next to the minimum_green_status subtracts the start time of min_green_status. Then add the minimum green time in the list.

My decision:

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) 

But the result is clearly erroneous, and it prints more start_time_green than the correct ones. I doubt the filter

  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')) 

or the logic about it is wrong.

 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, 806000, 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, 906000, 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'],] 
+4
source share
1 answer

This can be done using one cycle.

  • Initialize an empty green_time_list to store time for green intervals, green_on = False to indicate green is currently not enabled, and green_start is None.
  • If the green color is on, and the current status is not one of "0", "1", "3", then this is the first non-green record. Set the green value to off and calculate the time difference between the green_start and the timestamp of the current record. Add time difference to green_time_list
  • Otherwise, if green is off and the current status is one of "0", "1", "3", then this is the first "green" record. Set the green color to and save the timestamp in green_start_time

This, of course, assumes that your data is in chronological order.

 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 

Then for a given input, this returns

 >>> find_green_times(sg_status) ([datetime.timedelta(0, 94, 401000), datetime.timedelta(0, 390, 898000)], False, None) 
+2
source

All Articles