import re
urgency = ["asap", "now", "quickly"]
test = 'the patientneeds help/asap'
words = set(re.split(r"\W+", test))
if any(elem in words for elem in urgency):
print('Element Found')
import re
def is_urgent(sentence):
urgency = ["asap", "now", "quickly"]
return any(re.search(r'\b' + urgent_word + r'\b', sentence) for urgent_word in urgency)
tests = ['the patient needs help/asap', 'he needs help asap', 'wait until snowfall']
for test in tests:
if is_urgent(test):
print('URGENT:', test)
else:
print('Not urgent:', test)
输出:
URGENT: the patient needs help/asap
URGENT: he needs help asap
Not urgent: wait until snowfall
删除
split()
。但是in
在know
中now
将变成True
如果您想要特定的单词,并且想要避免像
snowfall
和know
这样的触发器,例如,您可以尝试以下方法:对于拆分“patientneeds”(没有字符分隔符连接的单词),似乎没有任何简单的解决方案,但这实际上为您提供了一个非常强大的机制
您应该使用正则表达式,并在每个搜索单词的开头和结尾添加一个单词边界
\b
的条件:输出:
相关问题 更多 >
编程相关推荐