2

我有一个文本句子为“我的父亲是美国人,他很帅”和“我的母亲来自北美,她很漂亮”。

我需要提取单词American(在这种情况下an)和America(在这种情况下)前面的单词North以显示到控制台。

注意:这个词America有一个后缀America + nAmerican在第二句中。

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

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

5 回答 5

4

这个怎么样?

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']
于 2013-08-10T18:14:47.027 回答
3

如果你要使用正则表达式,你的方法是不正确的。只需解析整个句子。前瞻断言会在Americaor之前给你这个词American

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']
于 2013-08-10T18:15:24.820 回答
1

像这样的东西?

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
于 2013-08-10T18:18:09.247 回答
0

你可以试试这个:

line = 'My Father is an American, and he is handsome'

words = line.split()
i = words.index("American,")
print words[i-1]

这将打印an

于 2013-08-10T18:27:19.400 回答
0

我不确定这些句子是如何分隔的,但如果它们在你可以使用的句子列表中。

import re
for line in sentences:
   sentence = line.strip().split(" ")
   for word in sentence:
       if re.search("America*",word):
           ind = sentence.index(word)
           print sentence[ind-1]
于 2013-08-10T18:46:32.527 回答