2

我面临着在数字序列中找到给定长度的不连续性(间隙)的问题。因此,例如,给定[1,2,3,7,8,9,10]和 的差距length=3,我会找到[4,5,6]。如果差距是length=4,我什么也找不到。当然,真正的序列要长得多。我在很多帖子中都看到了这个问题,它有各种应用程序和可能的实现。

我认为可能有效并且应该相对较快的一种方法是将完整集表示为一个位数组,其中包含 1 表示可用数字和 0 表示缺失 - 所以上面看起来像[1,1,1,0,0,0,1,1,1,1]。然后可能运行一个窗口函数,它将用完整的集合对给定长度的数组进行 XOR 掩码,直到所有位置的结果为 1。这将需要在大约 ~O(n) 内对整个序列进行单次传递,加上成本在每次运行中进行掩蔽。

这是我设法想出的:

def find_gap(array, start=0, length=10):
    """
    array:  assumed to be of length MAX_NUMBER and contain 0 or 1 
            if the value is actually present
    start:  indicates what value to start looking from
    length: what the length the gap should be
    """

    # create the bitmask to check against
    mask = ''.join( [1] * length )

    # convert the input 0/1 mapping to bit string
    # e.g - [1,0,1,0] -> '1010'
    bits =''.join( [ str(val) for val in array ] )

    for i in xrange(start, len(bits) - length):

        # find where the next gap begins
        if bits[i] != '0': continue

        # gap was found, extract segment of size 'length', compare w/ mask
        if (i + length < len(bits)):
            segment = bits[i:i+length]

            # use XOR between binary masks
            result  = bin( int(mask, 2) ^ int(segment, 2) )

            # if mask == result in base 2, gap found
            if result == ("0b%s" % mask): return i

    # if we got here, no gap exists
    return -1

这对于大约 100k(< 1 秒)来说相当快。我很感激有关如何使更大的集合更快/更有效的提示。谢谢!

4

5 回答 5

2

找到相邻数字之间的差异,然后寻找足够大的差异。我们通过构建两个列表来找到差异——除了第一个之外的所有数字,以及除了最后一个之外的所有数字——然后成对地减去它们。我们可以使用zip将值配对。

def find_gaps(numbers, gap_size):
    adjacent_differences = [(y - x) for (x, y) in zip(numbers[:-1], numbers[1:])]
    # If adjacent_differences[i] > gap_size, there is a gap of that size between
    # numbers[i] and numbers[i+1]. We return all such indexes in a list - so if
    # the result is [] (empty list), there are no gaps.
    return [i for (i, x) in enumerate(adjacent_differences) if x > gap_size]

(另外,学习一些 Python 习语。我们更喜欢直接迭代,并且我们有一个真正的布尔类型。)

于 2010-12-07T10:01:43.590 回答
2

您可以使用 XOR 和 shift 并且它确实在大约 O(n) 时间内运行。

然而,在实践中,建立一个索引(所有间隙的哈希列表大于某个最小长度)可能是一种更好的方法。

假设您从这些整数的序列(而不是位掩码)开始,那么您只需遍历序列即可构建索引;每当您发现大于阈值的间隙时,您都会将该间隙大小添加到字典中(如有必要,将其实例化为空列表,然后在序列中附加偏移量。

最后,您将获得序列中每个间隙(大于您所需的阈值)的列表。

这种方法的一个好处是您应该能够在修改基本列表时维护此索引。因此,构建索引所花费的 O(n*log(n)) 初始时间由后续查询和更新索引的 O(log(n)) 成本分摊。

这是一个非常粗略的构建函数gap_index()

def gap_idx(s, thresh=2):
    ret = dict()
    lw = s[0]  # initial low val.
    for z,i in enumerate(s[1:]):
        if i - lw < thresh:
            lw = i
            continue
        key = i - lw
        if key not in ret:
            ret[key] = list()
        ret[key].append(z)
        lw = i
    return ret

维护数据集和索引的类最好围绕内置的“对分”模块及其insort()功能构建。

于 2010-12-08T10:21:49.737 回答
1

These provide a single walk of your input list.

List of gap values for given length:

from itertools import tee, izip
def gapsofsize(iterable, length):
    a, b = tee(iterable)
    next(b, None)
    return ( p for x, y in izip(a, b) if y-x == length+1 for p in xrange(x+1,y) )

print list(gapsofsize([1,2,5,8,9], 2))

[3, 4, 6, 7]

All gap values:

def gaps(iterable):
    a, b = tee(iterable)
    next(b, None)
    return ( p for x, y in izip(a, b) if y-x > 1 for p in xrange(x+1,y) )

print list(gaps([1,2,4,5,8,9,14]))

[3, 6, 7, 10, 11, 12, 13]

List of gaps as vectors:

def gapsizes(iterable):
    a, b = tee(iterable)
    next(b, None)
    return ( (x+1, y-x-1) for x, y in izip(a, b) if y-x > 1 )

print list(gapsizes([1,2,4,5,8,9,14]))

[(3, 1), (6, 2), (10, 4)]

Note that these are generators and consume very little memory. I would love to know how these perform on your test dataset.

于 2010-12-07T10:51:00.343 回答
1

如果你追求的是效率,我会按照以下几行做一些事情(x序列号列表在哪里):

for i in range(1, len(x)):
  if x[i] - x[i - 1] == length + 1:
    print list(range(x[i - 1] + 1, x[i]))
于 2010-12-07T10:18:39.753 回答
1

几乎 aix 做了什么......但只得到所需长度的间隙:

def findGaps(mylist, gap_length, start_idx=0):
    gap_starts = []
    for idx in range(start_idx, len(mylist) - 1):
        if mylist[idx+1] - mylist[idx] == gap_length + 1:
            gap_starts.append(mylist[idx] + 1)

    return gap_starts

编辑:根据 OP 的意愿进行了调整。

于 2010-12-07T10:28:16.430 回答