给定2的整数列表如何找到不重叠的范围? [英] Given 2 list of integers how to find the non-overlapping ranges?

查看:83
本文介绍了给定2的整数列表如何找到不重叠的范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给予

x = [5, 30, 58, 72]
y = [8, 35, 53, 60, 66, 67, 68, 73]

目标是遍历x_i并找到大于x_i但不大于x_i+1

The goal is to iterate through x_i and find the value for y that's larger than x_i but not larger than x_i+1

假设两个列表都已排序且所有项目都是唯一的,则给定xy的期望输出为:

Assume that both list are sorted and all items are unique, the desired output given the x and y is:

[(5, 8), (30, 35), (58, 60), (72, 73)]

我尝试过:

def per_window(sequence, n=1):
    """
    From http://stackoverflow.com/q/42220614/610569
        >>> list(per_window([1,2,3,4], n=2))
        [(1, 2), (2, 3), (3, 4)]
        >>> list(per_window([1,2,3,4], n=3))
        [(1, 2, 3), (2, 3, 4)]
    """
    start, stop = 0, n
    seq = list(sequence)
    while stop <= len(seq):
        yield tuple(seq[start:stop])
        start += 1
        stop += 1

x = [5, 30, 58, 72]
y = [8, 35, 53, 60, 66, 67, 68, 73]

r = []

for xi, xiplus1 in per_window(x, 2):
    for j, yj in enumerate(y):
        if yj > xi and yj < xiplus1:
            r.append((xi, yj))
            break

# For the last x value.
# For the last x value.
for j, yj in enumerate(y):
    if yj > xiplus1:
        r.append((xiplus1, yj))
        break

但是有没有更简单的方法可以通过numpypandas或其他方式实现相同的目标?

But is there a simpler way to achieve the same with numpy, pandas or something else?

推荐答案

您可以将numpy.searchsortedside='right'结合使用,以找出y中大于x的第一个值的索引,然后提取具有索引的元素;一个简单的版本假定y中始终有一个值大于x 中的任何元素可以是:

You can use numpy.searchsorted with side='right' to find out the index of the first value in y that is larger than x and then extract the elements with the index; A simple version which assumes there is always one value in y larger than any element in x could be:

x = np.array([5, 30, 58, 72])
y = np.array([8, 35, 53, 60, 66, 67, 68, 73])

np.column_stack((x, y[np.searchsorted(y, x, side='right')]))
#array([[ 5,  8],
#       [30, 35],
#       [58, 60],
#       [72, 73]])


给出的y已排序:


Given y is sorted:

np.searchsorted(y, x, side='right')
# array([0, 1, 3, 7])

返回y中第一个值的索引,该索引大于x中的相应值.

returns the index of the first value in y that is larger than the corresponding value in x.

这篇关于给定2的整数列表如何找到不重叠的范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆