3

我的代码中的一个常见模式是:“搜索一个列表,直到找到一个特定元素,然后查看它之前和之后的元素。”

例如,我可能想查看一个日志文件,其中重要事件标有星号,然后提取重要事件的上下文。

在下面的例子中,我想知道光速引擎爆炸的原因:

  Spinning up the hyperdrive
  Hyperdrive speed 100 rpm
  Hyperdrive speed 200 rpm
  Hyperdrive lubricant levels low (100 gal.)
* CRITICAL EXISTENCE FAILURE
  Hyperdrive exploded

我想要一个函数 ,get_item_with_context()它允许我找到带星号的第一行,然后给我n它前面的行和它m后面的行。

我的尝试如下:

import collections, itertools
def get_item_with_context(predicate, iterable, items_before = 0, items_after = 0):
    # Searches through the list of `items` until an item matching `predicate` is found.
    # Then return that item.
    # If no item matching predicate is found, return None.
    # Optionally, also return up to `items_before` items preceding the target, and
    # `items after` items after the target.
    #
    # Note:
    d = collections.deque (maxlen = items_before + 1 + items_after)
    iter1 = iterable.__iter__()
    iter2 = itertools.takewhile(lambda x: not(predicate(x)), iter1)    
    d.extend(iter2)

    # zero-length input, or no matching item
    if len(d) == 0 or not(predicate(d[-1])):
        return None

    # get context after match:
    try:
        for i in xrange(items_after):
            d.append(iter1.next())
    except StopIteration:
        pass

    if ( items_before == 0 and items_after == 0):
        return d[0]
    else:
        return list(d)

用法应该是这样的:

>>> get_item_with_context(lambda x: x == 3, [1,2,3,4,5,6],
                          items_before = 1, items_after = 1)
[2, 3, 4]

这方面的问题:

  • 检查以确保我们确实找到了匹配项,使用not(predicate(d[-1])), 由于某种原因不起作用。它总是返回假。
  • 如果找到匹配项后列表中的项少于items_after项,则结果是垃圾。
  • 其他边缘情况?

我能否就如何使这项工作/使其更强大提供一些建议?或者,如果我正在重新发明轮子,也请随时告诉我。

4

8 回答 8

2

您可以使用collections.deque对象获取上下文的环形缓冲区。要获得 +/- 2 行上下文,请像这样初始化它:

context = collections.deque(maxlen=5)

然后遍历你喜欢的任何东西,为每一行调用这个:

context.append(line)

匹配context[2],并为每个匹配输出整个双端队列内容。

于 2012-05-03T09:53:39.990 回答
2

这似乎可以正确处理边缘情况:

from collections import deque

def item_with_context(predicate, seq, before=0, after=0):
    q = deque(maxlen=before)
    it = iter(seq)

    for s in it:
        if predicate(s):
            return list(q) + [s] + [x for _,x in zip(range(after), it)]
        q.append(s)
于 2012-05-03T13:11:23.337 回答
1

这可能是一个完全“unpythonic”的解决方案:

import itertools

def get_item_with_context(predicate, iterable, items_before = 0, items_after = 0):
    found_index = -1
    found_element = None

    before = [None] * items_before # Circular buffer

    after = []
    after_index = 0

    for element, index in zip(iterable, itertools.count()):
        if found_index >= 0:
            after += [element]
            if len(after) >= items_after:
                break
        elif predicate(element):
            found_index = index
            found_element = element
            if not items_after:
                break
        else:
            if items_before > 0:
                before[after_index] = element
                after_index = (after_index + 1) % items_before

    if found_index >= 0:
        if after_index:
            # rotate the circular before-buffer into place
            before = before[after_index:] + before[0:after_index]
        if found_index - items_before < 0:
            # slice off elements that "fell off" the start
            before = before[items_before - found_index:]
        return before, found_element, after

    return None

for index in range(0, 8):
    x = get_item_with_context(lambda x: x == index, [1,2,3,4,5,6], items_before = 1, items_after = 2)
    print(index, x)

输出:

0 None
1 ([], 1, [2, 3])
2 ([1], 2, [3, 4])
3 ([2], 3, [4, 5])
4 ([3], 4, [5, 6])
5 ([4], 5, [6])
6 ([5], 6, [])
7 None

我冒昧地更改了输出,以使与谓词匹配的内容以及之前和之后的内容更加清晰:

([2], 3, [4, 5])
  ^   ^    ^
  |   |    +-- after the element
  |   +------- the element that matched the predicate
  +----------- before the element

该函数处理:

  • 未找到项目,返回None(如果您想返回其他内容,则返回函数的最后一行)
  • 之前的元素没有完全实现(即找到的元素离开始太近而无法真正获得N它之前的元素)
  • 未完全实现的后元素(太接近结尾也是如此)
  • items_before 或 items_after 设置为 0(没有该方向的上下文)

它用:

  • 用于前元素的简单循环缓冲区,将其旋转到位以按正确顺序获取元素
  • 前元素的简单列表
  • 任何可迭代的,不需要可索引的集合,不会多次枚举任何元素,并且会在找到所需的上下文后停止
于 2012-05-03T09:39:17.853 回答
1
from itertools import takewhile, tee, chain
from collections import deque

def contextGet(iterable, predicate, before, after):
    iter1, iter2 = tee(iterable)

    beforeLog = deque(maxlen = before)
    for item in takewhile(lambda x: not(predicate(x)), iter1):
        beforeLog.append(item)
        iter2.next()

    afterLog = []
    for i in xrange(after + 1):
        try:
            afterLog.append(iter2.next())
        except StopIteration:
            break

    return chain(beforeLog, afterLog)

或者:

def contextGet(iterable, predicate, before, after):
    it1, it2 = tee(it)
    log = deque(maxlen = (before + after + 1))
    for i in chain(dropwhile(lambda x: not predicate(x), it1), xrange(after + 1)):
        try:
            log.append(it2.next())
        except StopIteration:
            break
    return log

after如果列表的其余部分比参数短,则第二个可能会返回太多“之前”元素。

于 2012-05-03T10:08:58.507 回答
0

我不确定我是否遗漏了问题中的某些内容,但这可以简单地完成为

>>> def get_item_with_context(predicate, iterable, items_before = 0, items_after = 0):
    queue = collections.deque(maxlen=items_before+1)
    found = False
    for e in iterable:
        queue.append(e)
        if not found and predicate(e):
            queue = collections.deque(queue,items_before+1+items_after)
            found = True
        if found:
            if not items_after : break
            items_after-=1
    if not found:
        queue.clear()
    return list(queue)

>>> get_item_with_context(lambda x: x == 0, [1,2,3,4,5,6],items_before = 2, items_after = 1)
[]
>>> get_item_with_context(lambda x: x == 4, [1,2,3,4,5,6],items_before = 2, items_after = 1)
[2, 3, 4, 5]
>>> get_item_with_context(lambda x: x == 1, [1,2,3,4,5,6],items_before = 2, items_after = 1)
[1, 2]
>>> get_item_with_context(lambda x: x == 6, [1,2,3,4,5,6],items_before = 2, items_after = 1)
[4, 5, 6]
>>> get_item_with_context(lambda x: x == 4, [1,2,3,4,5,6],items_before = 20, items_after = 10)
[1, 2, 3, 4, 5, 6]
于 2012-05-03T10:07:39.057 回答
0
import collections

def context_match(predicate, iterable, before = 0, after = 0):
    pre = collections.deque(maxlen = before + 1)
    post = []
    match = 0
    for el in iterable:
        if not match:
            pre.append(el)
            if predicate(el):
                match = 1
        elif match:
            if len(post) == after:
                break
            post.append(el)
    if not match:
        return
    output = list(pre)
    output.extend(post)
    return output

for val in xrange(8):
    print context_match(lambda x: x == val, [1,2,3,4,5,6],before = 2, after = 2)
#Output:
None
[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[2, 3, 4, 5, 6]
[3, 4, 5, 6]
[4, 5, 6]
None
于 2012-05-03T12:28:41.330 回答
0

这是更短的内容:

import collections
from itertools import islice

def windowfilter(pred, it, before=0, after=0):
        size = before + 1 + after
        q = collections.deque(maxlen=size)
        it = iter(it)
        for x in it:
                q.append(x)
                if pred(x):
                        # ok we got the item, add the trailing lines
                        more = list(islice(it, after))
                        q.extend(more)

                        # maybe there were too few items left
                        got = before + 1 + len(more)

                        # slice from the end
                        return tuple(q)[-got:]

测试产生:

seq = [1,2,3,4,5,6]
for elem in range(8):
        print elem, windowfilter((lambda x:x==elem), seq, 2, 1)

# Output:
0 None
1 (1, 2)
2 (1, 2, 3)
3 (1, 2, 3, 4)
4 (2, 3, 4, 5)
5 (3, 4, 5, 6)
6 (4, 5, 6)
7 None
于 2012-05-03T12:40:06.410 回答
0

我的答案是这样的,

for k,v in enumerate(iterable):
    #if cmp(v,predicate) == 0:
    if v == predicate:
        if k+items_after < len(iterable):
            res.append((' '.join(token[(k-items_before):(k+items_after+1)]))) 
        elif k+window == len(token):
            res.append((' '.join(token[(k-items_before):])))   
        else:
            res.append((' '.join(token[(k-items_before):])))

return res
于 2015-11-10T20:24:23.320 回答