在python中匹配/重叠数组。

时间:2022-02-24 22:05:36

I have two lists, one of integers and one of tuples containing integers as follows (this is an example)

我有两个列表,一个是整数,一个是包含整数的元组(这是一个例子)

firstList = [4, 8, 20, 25, 60, 123]
secondList = [(0, 3), (4, 5), (7, 14), (19, 22), (40, 90), (100, 140)]

I am wanting to match up (overlap) the first array onto the second array as well as possible. For example:

我想要将第一个数组和第一个数组匹配起来。例如:

The first section of the array [4, 8, 20] would match up with [(4, 5), (7, 14), (19, 22)] as those first numbers fall on or between the numbers in their respective tuples.

数组的第一部分[4,8,20]将与[(4,5),(7,14),(19,22)]匹配,因为第一个数字是在它们各自的元组中出现或在数字之间。

25 would be ignored as there is no match for it

25将被忽略,因为没有匹配。

[60, 123] would then match up with [(40, 90), (100, 140)]

[60,123]然后匹配[(40,90),(100,140)]

The above is fairly simple to implement however what I am getting stuck with is where there is an offset that throws everything off. Say 4 is subtracted from all the numbers in the first array so we are now left with

上面的操作很简单,但是我要做的是,有一个偏移量,把所有的东西都抛掉,比如4,从第一个数组的所有数字中减去,所以我们现在就剩下了。

firstList = [0, 4, 16, 21, 56, 119]
secondList = [(0, 3), (4, 5), (7, 14), (19, 22), (40, 90), (100, 140)]

Some numbers still match up, however not all of them and now 21 falls within the range of (19, 22) when it should be ignored and a number of other numbers fall within the wrong ranges now. How can these new numbers still be matched up in the "best possible way" in other words in a fashion in which there are the most matches?

一些数字仍然匹配,但并不是所有的数字,现在21是在(19,22)的范围内,当它应该被忽略时,其他一些数字现在在错误的范围内。这些新的数字怎么能以“最佳的方式”匹配,换句话说,在一种最匹配的方式中呢?

Ideally the method would figure out to add an offset back 4 and then match up the numbers again.

理想情况下,该方法将计算出一个抵消后4,然后再匹配数字。


Clarification edit The numbers and tuples will be ordered in ascending sequence (as seen above), any given number can't be matched with multiple tuples and any given tuple can't be matched with multiple numbers. It's 1 to 1.

对数字和元组的解释将按升序排列(如上所示),任何给定的数字都不能与多个元组匹配,任何给定的元组不能与多个数字匹配。这是1比1。

The three things I am wanting returned from this are 1: The offset (4) in this case that gets things matched up the best. 2: What are the numbers that match up with a tuple and which tuple do they match up with. 3: What numbers don't match up in the best case scenerio (with the most matches).

我想从这里得到的三样东西是1:在这种情况下,抵消(4)得到的结果是最好的。2:哪些数字与一个元组匹配,哪些元组匹配。3:在最好的情况下,哪些数字不匹配?

Here's an illustration, I don't know if this will help though. Imagine a long piece of paper along a table with the number line. The sections indicated by the tuples have stripes of colour painted along them (each number section a different colours) painted along them (e.g. red strip from 0 to 3, purple strip from 4 to 5, etc). The areas that don't have any number section are left blank (or white)

这里有个例子,我不知道这会不会有帮助。想象一张有数轴的桌子。图中所示的部分是沿着它们绘制的彩色条纹(每个数字部分有不同的颜色)(比如从0到3的红色条,从4到5的紫色条等等)。没有数字部分的区域是空白的(或白色)

Then you have a second paper (also with a printed numberline) placed above this first one with holes cut out at the given numbers in firstList Where should you slide this top numberline over the stationary bottom numberline so as to see colours in as many of the holes as possible ensuring that the same colour does not appear twice?

然后你有一个第二篇论文(也与印刷numberline)上面放置第一个洞剪在给定的数字应该在firstList幻灯片上面numberline在固定底部numberline以看到颜色尽可能多的孔确保没有出现两次相同的颜色?

I don't know if that makes things any clearer - I hope it does!

我不知道这是否能让事情变得更清楚——我希望它能做到!

Is there a library/algorithm or method for performing such a task apart from brute forcing it?

是否有一个库/算法或方法来执行这样的任务,而不是强制执行?

1 个解决方案

#1


0  

Your question isn't fully defined, but here's something that makes sense to me:

你的问题还没有完全定义,但我想说的是:

def binSearch(x, L, low=None, high=None):
    if low is None: low = 0
    if high is None: high = len(L)
    if low==high: return None

    mid = int((upper+lower)/2)
    lower, upper = L[mid]

    if lower <= x <= upper: return mid

    if x>high: return binSearch(x, L, mid+1, high)
    return binSearch(x, L, low, mid)


def match(L, T):
    answer = {}
    for num in L:
        m = binSearch(num, T)
        if m is None: continue
        answer[num] = L[m]
        L.pop(m)
    return answer


def main():
    firstList = [4, 8, 20, 25, 60, 123]
    secondList = [(0, 3), (4, 5), (7, 14), (19, 22), (40, 90), (100, 140)]
    matches = match(firstList, secondList)
    for k in sorted(matches):
        print(k, "matches with", matches[k])

#1


0  

Your question isn't fully defined, but here's something that makes sense to me:

你的问题还没有完全定义,但我想说的是:

def binSearch(x, L, low=None, high=None):
    if low is None: low = 0
    if high is None: high = len(L)
    if low==high: return None

    mid = int((upper+lower)/2)
    lower, upper = L[mid]

    if lower <= x <= upper: return mid

    if x>high: return binSearch(x, L, mid+1, high)
    return binSearch(x, L, low, mid)


def match(L, T):
    answer = {}
    for num in L:
        m = binSearch(num, T)
        if m is None: continue
        answer[num] = L[m]
        L.pop(m)
    return answer


def main():
    firstList = [4, 8, 20, 25, 60, 123]
    secondList = [(0, 3), (4, 5), (7, 14), (19, 22), (40, 90), (100, 140)]
    matches = match(firstList, secondList)
    for k in sorted(matches):
        print(k, "matches with", matches[k])