python - 如何在我的 doctest 中只为完整的单词匹配返回 TRUE?
问题描述
TRUE
如果物种名称以开头,我有一个 doctest 要返回,但如果给定的物种名称包含拼写错误(例如)quercus
,它也会返回。我如何确保它不仅以该物种名称开头并且仅与完整单词匹配?TRUE
quercuss
TRUE
def is_an_oak(name):
""" Returns True if name is starts with 'quercus'"""
return name.lower().startswith('quercus')
我尝试在单词后留一个空格,但在脚本中进一步使用该函数时它省略了结果:
def is_an_oak(name):
""" Returns True if name is starts with 'quercus'"""
return name.lower().startswith('quercus ')
解决方案
为了得到这个词;
import re
def is_an_oak(name):
""" Returns Name if name is 'quercus'"""
return re.match("^[qQ]uercus*",name).string
为了获得 TRUE/FALSE
import re
def is_an_oak(name):
""" Returns True if name is 'quercus'"""
if re.match("^[qQ]uercus*",name):
return True
else:
return False
推荐阅读
- c - 如何设置 SUID 位以授予 root 权限?
- security - AbpAuditlogs 中记录了哪些可疑活动?他们的意图是什么?
- laravel-livewire - Livewire 按钮不会点击
- neo4j - 根据 graphQL 模式验证 neo4j 数据库
- vue.js - Vue.js:配置 Router.js 后出错。Vue/类型/umd。chokidar(C 驱动器错误)
- python - SSLContext 超过最大递归深度(eventlet、flask、flask-socketio、tweepy)
- android - 使用原始证书重新编译apk
- gradle - 使用 Gradle 完成多级目录项目
- r - Cmake 错误:无法加载缓存。故障排除建议?
- c++ - Ncurses mvwprintw 不打印