如何在python中的字符串中获取特定单词旁边的单词的列表

时间:2018-10-31 14:45:04

标签: python python-3.x

假设我有一个字符串 string = 'i am a person i believe i can fly i believe i can touch the sky'

我想做的是得到所有{strong> all (在右侧)单词'i'旁边的单词,因此在本例中为am, believe, can, believe, can

我该如何在python中这样做?我找到了this,但它只给出了第一个单词,因此在这种情况下,'am'

5 个答案:

答案 0 :(得分:2)

简单的生成器方法:

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

用法:

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

答案 1 :(得分:1)

您可以编写一个正则表达式来查找目标词之后的词:

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']

答案 2 :(得分:1)

您可以split字符串,并在enumerate进行迭代时获得单词“ i”的下一个索引:

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']

请注意,正如@PatrickHaugh所指出的那样,如果要以“ i”作为最后一个词,我们要格外小心,以便我们完全排除对最后一个词的迭代。

答案 3 :(得分:1)

一种方法是在断言后使用regular expression

>>> 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']

答案 4 :(得分: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)
相关问题