-1

Say I have a list...

['a','brown','cat','runs','another','cat','jumps','up','the','hill']

...and I want to go through that list and return all instances of a specific item as well as the 2 items leading up to and proceeding that item. Exactly like this if I am searching for 'cat'

[('a','brown','cat','runs','another'),('runs','another','cat','jumps','up')]

the order of the returned list of tuples is irrelevant, ideally the code handle instances where the word was the first or last in a list, and an efficient and compact piece of code would be better of course.

Thanks again everybody, I am just getting my feet wet in Python and everybody here has been a huge help!

  • http://stackoverflow.com/questions/176918/finding-the-index-of-an-item-given-a-list-containing-it-in-python – karthikr Jun 06 '14 at 02:57

3 Answers3

2

Without error checking:

words = ['a','brown','cat','runs','another','cat','jumps','up','the','hill']
the_word = 'cat'

seqs = []

for i, word in enumerate(words):
    if word == the_word:
        seqs.append(tuple(words[i-2:i+3]))

print seqs #Prints: [('a', 'brown', 'cat', 'runs', 'another'), ('runs', 'another', 'cat', 'jumps', 'up')]
Dair
  • 15,910
  • 9
  • 62
  • 107
2

A recursive solution:

def context(ls, s): 
    if not s in ls: return []
    i = ls.index('cat')
    return [ tuple(ls[i-2:i+3]) ] + context(ls[i + 1:], s)

ls = ['a','brown','cat','runs','another','cat','jumps','up','the','hill']

print context(ls, 'cat')

Gives:

[('a','brown','cat','runs','another'),('runs','another','cat','jumps','up')]
perreal
  • 94,503
  • 21
  • 155
  • 181
2

With error checking:

def grep(in_list, word):
    out_list = []
    for i, val in enumerate(in_list):
        if val == word:
            lower = i-2 if i-2 > 0 else 0
            upper = i+3 if i+3 < len(in_list) else len(in_list)
            out_list.append(tuple(in_list[lower:upper]))
    return out_list

in_list = ['a', 'brown', 'cat', 'runs', 'another', 'cat', 'jumps', 'up', 'the', 'hill']

grep(in_list, "cat")
# output: [('a', 'brown', 'cat', 'runs', 'another'), ('runs', 'another', 'cat', 'jumps', 'up')]

grep(in_list, "the")
# output: [('jumps', 'up', 'the', 'hill')]
dano
  • 91,354
  • 19
  • 222
  • 219