Match the item in the list, then return the `n` elements before it and the` m` after it

In my code there is a regular template: "search through the list until I find a specific item, and then look at the items that appear before and after it."

As an example, I can view a log file where important events are marked with asterisks, and then pull out the context of an important event.

In the following example, I want to know why the hyperdrive exploded:

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

I need a function get_item_with_context()that allows me to find the first line with an asterisk, and then returns me the lines npreceding it and the lines mfollowing it.

My attempt below:

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)

Usage should look like this:

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

Problems with this:

  • , , , not(predicate(d[-1])), - . false.
  • items_after , , .
  • ?

, / ? , , .

+3
8

, :

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)
+2

, collections.deque. +/- 2 , :

context = collections.deque(maxlen=5)

, , :

context.append(line)

context[2] deque .

+2

, , "" :

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 ( )

:

  • -, ,
  • before-elements
  • , , -
+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.

+1

, - ,

>>> 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]
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
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
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
0
source

All Articles