【发布时间】:2019-07-29 23:26:52
【问题描述】:
我想检查一个连字符的单词是否以以下集合中的前缀开头。例如,“脱盐”。
prefixes = {
'de-', 'dis-', 'il-', 'im-', 'ir-', 'inter-',
'mid-', 'mis-', 'non-', 'pre-', 'pro-', 're-',
'semi-', 'sub-', 'tele-', 'trans-',
'un-', 'e-'
}
这是我的代码:
def prefix(word):
match = re.match(r"[a-z]+-",word)
if match:
if match.group() in prefixes:
return True
word = "e-mail"
print(prefix(word))
【问题讨论】:
-
你想让“电话”匹配吗?
-
这已经有答案here它是python内置的
-
没有。所有单词都是连字符。
-
不错。谢谢!
-
这个问题是针对编码效率还是运行时效率? [但是,如果有比 Jab 的评论更好的答案,我会感到惊讶。]