【问题标题】:Numpy: given a set of ranges, is there an efficient way to find the set of ranges that are disjoint with all other ranges?Numpy:给定一组范围,是否有一种有效的方法来找到与所有其他范围不相交的范围集?
【发布时间】:2023-01-23 23:40:07
【问题描述】:

有没有一种优雅的方法可以从 numpy 的一组范围中找到一组不相交的范围?

disjoint_ranges = [] # these are all disjoint
adjoint_ranges = [] # these do not all have to be mutually adjoint
for index, range_1 in enumerate(ranges):
    i, j = range_1 # all ranges are ordered s.t. i<j
    for swap_2 in ranges[index+1:]: # the list of ranges is ordered by increasing i
        a, b, _ = swap_2
        if a<j and a>i:
            adjoint_swaps.append(swap)
            adjoint_swaps.append(swap_2)
    else:
        if swap not in adjoint_swaps:
            swaps_to_do.append(swap)
print(adjoint_swaps)
print(swaps_to_do)

【问题讨论】:

  • 为清楚起见,请提供最小的输入/输出示例
  • 输入也很重要...

标签: python numpy


【解决方案1】:

我不确定 numpypandas 有以下内容:

from functools import reduce
import pandas as pd

ranges = [
    pd.RangeIndex(10, 20),
    pd.RangeIndex(15, 25),
    pd.RangeIndex(30, 50),
    pd.RangeIndex(40, 60),
]

disjoints = reduce(lambda x, y : x.symmetric_difference(y), ranges)
disjoints
Int64Index([10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32, 33, 34, 35, 36,
            37, 38, 39, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
           dtype='int64')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    相关资源
    最近更新 更多