0

I have a nested list:

[['2012 Mar 03 23:39:46', '2012 Mar 03 23:39:58'], ['2012 Mar 03 23:32:46', '2012 Mar 03 23:32:46'], ['2012 Mar 03 23:12:19', '2012 Mar 03 23:12:19'], ['2012 Mar 03 23:06:50', '2012 Mar 03 23:08:03'], ['2012 Mar 03 22:57:28', '2012 Mar 03 22:57:28'], ['2012 Mar 03 22:47:00', '2012 Mar 03 22:47:00'], ['2012 Mar 03 22:45:45', '2012 Mar 03 22:45:45'], ['2012 Mar 03 22:37:41', '2012 Mar 03 22:37:41'], ['2012 Mar 03 22:35:15', '2012 Mar 03 22:35:15'], ...]

I would like to find the highest number when these time ranges overlap each other (for example when there are 3 time periods that overlap each other, I would like to get "3"). This list is sorted in descending order.

I wrote this code:

counter=list()
for i in range(len(sorted_times)):
    j=i
    while j < len(sorted_times):
        temp_counter=0
        if sorted_times[i][0] <= sorted_times[j][1]:
            temp_counter +=1
            j +=1
        else:
            counter.append(temp_counter)
            break

But the list counter is only filled with 0. I solved this problem before by using the pandas and numpy libraries, and I know that there should be 2 time ranges that are overlapping. This time I would like to do this using only built-in python libraries (except the time or datetime libraries).

I don't know what is wrong with this code. It should at least return a list full of 1s, if there are no overlapping time periods.

I hope I explained my problem clearly enough. It's my first post here, so I'm sorry in advance if something is missing here.

EDIT

As Tim Castelijns sugested, this is my list with timestamps (float numbers) which I also tried:

[[1330814386.0, 1330814398.0], [1330813966.0, 1330813966.0], [1330812739.0, 1330812739.0], [1330812410.0, 1330812483.0], [1330811848.0, 1330811848.0], [1330811220.0, 1330811220.0], [1330811145.0, 1330811145.0], [1330810661.0, 1330810661.0], [1330810515.0, 1330810515.0], [1330810361.0, 1330810417.0], [1330809037.0, 1330809037.0], [1330808213.0, 1330808213.0], [1330807897.0, 1330807897.0],

And I found out why my code didn't work properly - I should wrote:

temp_counter=0
while j < len(sorted_times):

instead of:

while j < len(sorted_times):
    temp_counter=0

Stupid mistake, which I couldn't see for one day :(

Unfortunately, I still didn't get the same answer as using pandas. When I work with bigger data sample (over 3000) using pandas I get the number 9 as the answer and using built-in libraries I get 5. Is there any difference between using time.strftime() or not converted timestamps in seconds and using pandas.to_datetime(df.timestamp, unit='s')? I know that the time zone can be different, but it shouldn't be a problem in this case.

Aggie
  • 1
  • 3

0 Answers0