嗨,我是编程新手,我必须用python做一个语言处理任务。我试图做的是我有一个名词列表,例如
nouns=['hotel','staff','pool']
和需要根据文本中的每个单词是否可以在名词列表中找到来标记的文本?例如
text=['the','hotel','is','really','nice','.']
如果文本中的每个元素都可以在名词列表中找到,则输入yes,否则输入no。理想输出如下所示。
output=[('the','no'),('hotel','yes'),('is','no'),('really','no'),('nice','no'),('.','no')]
答案 0 :(得分:3)
您希望更好地搜索名词,使其成为set
。 list
会执行线性搜索。请参阅time complexity:
>>> nouns = {'hotel', 'room', 'staff', 'pool'}
>>> text = ['the', 'hotel', 'is', 'really', 'nice', '.']
>>> [(word, 'yes' if word in nouns else 'no')
... for word in text]
[('the', 'no'),
('hotel', 'yes'),
('is', 'no'),
('really', 'no'),
('nice', 'no'),
('.', 'no')]
答案 1 :(得分:1)
假设您忘记了hotel
列表中的nouns
:
<强>脚本:强>
nouns = {'room','staff','pool','hotel'}
text = ['the','hotel','is','really','nice','.']
out = [(word, 'yes' if word in nouns else 'no') for word in text]
print(out)
或者黑客的方式:更短:
nouns = {'room','staff','pool','hotel'}
text = ['the','hotel','is','really','nice','.']
out = [(word, ['no','yes'][word in nouns]) for word in text]
print(out)
<强>输出:强>
[
('the', 'no'),
('hotel', 'yes'),
('is', 'no'),
('really', 'no'),
('nice', 'no'),
('.', 'no')
]
答案 2 :(得分:0)
这个怎么样?
nouns=['room','staff','pool']
text=['the','hotel','is','really','nice','.']
[(n, 'yes') if n in nouns else (n, 'no') for n in text]