python中关于闭合器的应用
Posted yuanmq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python中关于闭合器的应用相关的知识,希望对你有一定的参考价值。
import re
def build_match_and_apply_functions(pattern, search, replace):
def matches_rule(word):
return re.search(pattern, word)
def apply_rule(word):
return re.sub(search, replace, word)
return (matches_rule, apply_rule)
patterns =
(
(‘[sxz]$‘, ‘$‘, ‘es‘),
(‘[^aeioudgkprt]h$‘, ‘$‘, ‘es‘),
(‘(qu|[^aeiou])y$‘, ‘y$‘, ‘ies‘),
(‘$‘, ‘$‘, ‘s‘)
)
rules = [build_match_and_apply_functions(pattern, search, replace)
for (pattern, search, replace) in patterns]
def plural(noun):
for matches_rule, apply_rule in rules:
if matches_rule(noun):
return print(apply_rule(noun))
以上是关于python中关于闭合器的应用的主要内容,如果未能解决你的问题,请参考以下文章