-2

Assuming I have a string string = 'i am a person i believe i can fly i believe i can touch the sky'.

What I would like to do is to get all the words that are next to (from the right side) the word 'i', so in this case am, believe, can, believe, can.

How could I do that in python ? I found this but it only gives the first word, so in this case, 'am'

quant
  • 4,062
  • 5
  • 29
  • 70

5 Answers5

2

Simple generator method:

def get_next_words(text, match, sep=' '):
    words = iter(text.split(sep))
    for word in words:
        if word == match:
            yield next(words)

Usage:

text = 'i am a person i believe i can fly i believe i can touch the sky'
words = get_next_words(text, 'i')

for w in words:
    print(w)

# am
# believe
# can
# believe
# can
Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
1

You can write a regular expression to find the words after the target word:

import re

word = "i"
string = 'i am a person i believe i can fly i believe i can touch the sky'

pat = re.compile(r'\b{}\b \b(\w+)\b'.format(word)) 
print(pat.findall(string))
# ['am', 'believe', 'can', 'believe', 'can']
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
1

You can split the string and get the next index of the word "i" as you iterate with enumerate:

string = 'i am a person i believe i can fly i believe i can touch the sky'

sl = string.split()
all_is = [sl[i + 1] for i, word in enumerate(sl[:-1]) if word == 'i']
print(all_is)
# ['am', 'believe', 'can', 'believe', 'can']

Note that as @PatrickHaugh pointed out, we want to be careful if "i" is the last word so we can exclude iterating over the last word completely.

slider
  • 12,810
  • 1
  • 26
  • 42
1

One way is to use a regular expression with a look behind assertion:

>>> import re
>>> string = 'i am a person i believe i can fly i believe i can touch the sky'
>>> re.findall(r'(?<=\bi )\w+', string)
['am', 'believe', 'can', 'believe', 'can']
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
0
import re
string = 'i am a person i believe i can fly i believe i can touch the sky'
words = [w.split()[0] for w in re.split('i +', string) if w]
print(words)
Hkoof
  • 756
  • 5
  • 14