Find the sentence’s index (sentences in a list) of a specific word in Python

3.2k Views Asked by At

i currently have a file that contains a list that is looks like

example = ['Mary had a little lamb' , 
       'Jack went up the hill' , 
       'Jill followed suit' ,    
       'i woke up suddenly' ,
       'it was a really bad dream...']

I would like to find the index of the sentence with the word “woke” by example. In this example the answer should be f(“woke”)=3. F is a function.

I tried to tokenize each sentence to first find the index of the word like that:

>>> from nltk.tokenize import word_tokenize
>>> example = ['Mary had a little lamb' , 
...            'Jack went up the hill' , 
...            'Jill followed suit' ,    
...            'i woke up suddenly' ,
...            'it was a really bad dream...']
>>> tokenized_sents = [word_tokenize(i) for i in example]
>>> for i in tokenized_sents:
...     print i
... 
['Mary', 'had', 'a', 'little', 'lamb']
['Jack', 'went', 'up', 'the', 'hill']
['Jill', 'followed', 'suit']
['i', 'woke', 'up', 'suddenly']
['it', 'was', 'a', 'really', 'bad', 'dream', '...']

But I don’t know how to finally get the index of the word and how to link it to the sentence’s index. Does someone know how to do that?

3

There are 3 best solutions below

3
On BEST ANSWER

You can iterate over each string in the list, split on white space, then see if your search word is in that list of words. If you do this in a list comprehension, you can return a list of indices to the strings that satisfied this requirement.

def f(l, s):
    return [index for index, value in enumerate(l) if s in value.split()]

>>> f(example, 'woke')
[3]
>>> f(example, 'foobar')
[]
>>> f(example, 'a')
[0, 4]

If you prefer using the nltk library

def f(l, s):
    return [index for index, value in enumerate(l) if s in word_tokenize(value)]
0
On
for index, sentence in enumerate(tokenized_sents):
    if 'woke' in sentence:
        return index

For all the sentences:

return [index for index, sentence in enumerate(tokenized_sets) if 'woke' in sentence]
0
On

If the requirement is to return the first sentence with the occurence of that word you can use something like -

def func(strs, word):
    for idx, s in enumerate(strs):
        if s.find(word) != -1:
            return idx
example = ['Mary had a little lamb' , 
       'Jack went up the hill' , 
       'Jill followed suit' ,    
       'i woke up suddenly' ,
       'it was a really bad dream...']
func(example,"woke")