Skip to content Skip to sidebar Skip to footer

How I Display 2 Words Before And After A Key Search Word In Python

Very new to Python programming. How I display 2 words before and after a key search word. In below example I am looking for a search word = lists Sample: Line 1: List of the keyb

Solution 1:

Through re.findall function.

>>> s = """List of the keyboard shortcuts for Word 2000
Sequences: strings, lists, and tuples - PythonLearn""">>> re.findall(r'\S+ \S+ \S*\blists\S* \S+ \S+', s)
['Sequences: strings, lists, and tuples']

Without regex.

>>> s = """List of the keyboard shortcuts for Word 2000
Sequences: strings, lists, and tuples - PythonLearn""">>> for i in s.split('\n'):
        z = i.split()
        for x,y inenumerate(z):
            if'lists'in y:
                print(z[x-2]+' '+z[x-1]+' '+z[x]+' '+z[x+1]+' '+z[x+2])


Sequences: strings, lists, and tuples

Solution 2:

This solution is based on Avinash Raj's second example with these amendments:

  • Allows the number of words to be printed each side of the search word to be varied
  • Uses a list comprehension instead of if inside for, which may be considered more 'Pythonic', though I'm not sure in this case if it's more readable.

.

s = """List of the keyboard shortcuts for Word 2000
Sequences: strings, lists and tuples - PythonLearn"""
findword = 'lists'
numwords = 2for i in s.split('\n'):
    z = i.split(' ')

    for x in [x for (x, y) inenumerate(z) if findword in y]:
        print(' '.join(z[max(x-numwords,0):x+numwords+1]))

Solution 3:

This is the solution I can think of right away for your question :-)

def get_word_list(line, keyword, length, splitter):
    word_list = line.split(keyword)
    if len(word_list) == 1:
        return []
    search_result = []
    temp_result = ""index = 0whileindex < len(word_list):
        result = word_list[index].strip().split(splitter, length-1)[-1]
        result += " " + keyword
        ifindex+1 > len(word_list):
            search_result.append(result.strip())
            break
        right_string = word_list[index+1].lstrip(" ").split(splitter, length+1)[:length]
        print word_list[index+1].lstrip(), right_string
        result += " " + " ".join(right_string)
        search_result.append(result.strip())
        index += 2return search_result

def search(file, keyword, length=2, splitter= " "):
    search_results = []
    with open(file, "r") as fo:
        for line in fo:
            line = line.strip()
            search_results += get_word_list(line, keyword, length, splitter)
    for result in search_results:
        print"Result:", result

Post a Comment for "How I Display 2 Words Before And After A Key Search Word In Python"