python - 获取日期时间范围列表的并集和交集 python

标签 python datetime union intersection

我有两个日期时间范围列表。 例如。

l1 = [(datetime.datetime(2018, 8, 29, 1, 0, 0), datetime.datetime(2018, 8, 29, 3, 0, 0)), (datetime.datetime(2018, 8, 29, 6, 0, 0), datetime.datetime(2018, 8, 29, 9, 0, 0))]
l2 = [(datetime.datetime(2018, 8, 29, 2, 0, 0), datetime.datetime(2018, 8, 29, 4, 0, 0)), (datetime.datetime(2018, 8, 29, 5, 0, 0), datetime.datetime(2018, 8, 29, 7, 0, 0))]

我想得到l1l2的并集。 所需的输出是:

union = [(datetime.datetime(2018, 8, 29, 1, 0, 0), datetime.datetime(2018, 8, 29, 4, 0, 0)), (datetime.datetime(2018, 8, 29, 5, 0, 0), datetime.datetime(2018, 8, 29, 9, 0, 0))]
intersection = [(datetime.datetime(2018, 8, 29, 2, 0, 0), datetime.datetime(2018, 8, 29, 3, 0, 0)), (datetime.datetime(2018, 8, 29, 6, 0, 0), datetime.datetime(2018, 8, 29, 7, 0, 0))]

真实数据可能不会如此完美地对齐。

最佳答案

答案here对于您所要求的内容非常有用,因为它可以压缩重叠范围的数组:

from operator import itemgetter

def consolidate(intervals):
    sorted_intervals = sorted(intervals, key=itemgetter(0))

    if not sorted_intervals:  # no intervals to merge
        return

    # low and high represent the bounds of the current run of merges
    low, high = sorted_intervals[0]

    for iv in sorted_intervals[1:]:
        if iv[0] <= high:  # new interval overlaps current run
            high = max(high, iv[1])  # merge with the current run
        else:  # current run is over
            yield low, high  # yield accumulated interval
            low, high = iv  # start new run

    yield low, high  # end the final run

l1l2 的并集只是 l1l2 中所有范围的合并:

def union(l1, l2):
    return consolidate([*l1, *l2])

l1l2 的交集由 AChampion 的代码充分完成(如果 l1 中的任何范围与 l1 中的任何范围之间存在任何重叠) l2,重叠应该出现在结果中),但它可能会导致范围碎片;我们可以使用相同的函数来连接相邻或重叠的范围:

from itertools import product

def intersection(l1, l2):
    result = ((max(s1, s2), min(e1, e2)) for (s1, e1), (s2, e2) in product(l1, l2) if s1 < e2 and e1 > s2)
    return consolidate(result)

一个例子:

l1 = [(1, 7), (4, 8), (10, 15), (20, 30), (50, 60)]
l2 = [(3, 6), (8, 11), (15, 20)]
print(list(union(l1, l2)))         # [(1, 30), (50, 60)]
print(list(intersection(l1, l2)))  # [(3, 6), (10, 11)]

(为了清楚起见,该示例使用整数,但它适用于任何类似的类型。具体来说,对于 OP 的 l1l2,代码生成 OP 所需的 datetime 结果。)

关于python - 获取日期时间范围列表的并集和交集 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52073609/

相关文章:

python - 我的 python 计算器返回一个字符串,而不是正确的数学运算序列

python - 如何只将具有最大值的行保留在同一类型项目的列中?

mysql - 这个 UNION ALL mysql 查询有什么问题?

mysql - 如何修复 mysql 错误号 #1250 SELECT 之一的表 'a1' 不能在字段列表中使用?

python - 通过 BigQuery python api 查询联合表 (Google Drive)

python - 未能在qt快速应用程序中构建图形管道状态

python - Pandas 时间戳到 datetime.datetime()

python - 使用 Python 设置文件中日期字符串的格式

node.js - Sequelize 未能更新 SQL Server 中的日期时间数据类型

sql - 在 postgresql 中获取空/打开时间范围