我有一个文字句子,“我的父亲是美国人,他很帅”,“我母亲来自北美,她很好”。
我需要提取单词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*'
答案 0 :(得分: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']
答案 1 :(得分:3)
如果要使用正则表达式,则表示方法不正确。只解析整个句子。前瞻性断言会在America
或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']
答案 2 :(得分: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
答案 3 :(得分:0)
line = 'My Father is an American, and he is handsome'
words = line.split()
i = words.index("American,")
print words[i-1]
这将打印an
答案 4 :(得分: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]