识别给定单词序列前面的字符串

2024-05-16 14:25:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个短信句子:“我爸爸是美国人,他很帅”和“我妈妈来自北美,她很好”。你知道吗

我需要提取要显示到控制台的单词American(在本例中为an)和America(在本例中为North)前面的单词。你知道吗

注意:单词America在第二句中有一个后缀America + n,使它成为American。你知道吗

到目前为止我的代码:::

for line in words:
    for word in line.strip().split(' '):
         // HERE I SHOULD WRITE THE CODE TO IDENTIFY THE WORD BEFORE THE STRING 'AMERICA*'

Tags: theinanforline单词短信后缀
3条回答

如果你要使用正则表达式,你的方法是不正确的。把整个句子分析一下。前瞻断言将为您提供AmericaAmerican之前的单词:

re.findall(r'\w+(?=\s+American?)', line)

演示:

>>> line = 'My Father is an American, and he is handsome'
>>> re.findall(r'\w+(?=\s+American?)', line)
['an']
>>> line = 'My Mother is from North America and she is nice'
>>> re.findall(r'\w+(?=\s+American?)', line)
['North']

这也适用于整个文本:

>>> text = '''\
... My Father is an American, and he is handsome
... My Mother is from North America and she is nice
... '''
>>> re.findall(r'\w+(?=\s+American?)', text)
['an', 'North']

像这样的?你知道吗

x='My Father is an American, and he is handsome. My Mother is from North America and she is nice'

y = x.split()[1:]
for (i,j) in enumerate(y):
    if j.startswith('America'):
        print y[i-1]

an
North

这个怎么样?你知道吗

import re

s = """
My Father is an American, and he is handsome
My Mother is from North America and she is nice
"""

print re.findall(r"(\w+)\sAmerica", s)

印刷品:

['an', 'North']

相关问题 更多 >