Search for total duration in non-overlapping time series data

I have a list of records with start and end timestamps, for example:

[{u'end_time': 1444197616.0, u'start_time': u'2015-10-07T05:59:58Z'}, ...]

This time period may overlap, and I cannot figure out how to find the total length of time without overlapping. For example:

BEGIN END
03:00 03:03
03:02 03:05
03:04 03:05
03:01 03:06
03:08 03:10

0 - 3
  2    -  5
      4 - 5
 1    -     6
               8 - 10

Total = 8 minutes.

I think I can do this by finding spaces, but how?

+4
source share
1 answer

I will get ranges in regular floats, and it should extend to datetime quite easily ...

Suppose the data is a sequence of [start, end]lists. If it is not in this format, you can easily convert it to this format.

. , , i + 1 i, i, .

. , . , , . , :

def get_collapsed_ranges(ranges):
    ranges = iter(sorted(ranges))
    current_range = next(ranges)
    for start, end in ranges:
        if start > current_range[1]:
            yield current_range
            current_range = [start, end]
        elif end > current_range[1]:
            current_range[1] = end
    yield current_range

, , , :

>>> list(get_collapsed_ranges([[0,3], [2,5], [4,5], [1,6], [8,10]]))
[[0, 6], [8, 10]]

, , , .

0

Source: https://habr.com/ru/post/1610537/


All Articles