简化以下方法

时间:2019-11-11 09:15:09

标签: python python-3.x methods

以下方法需要简化,以使行变得相对较小。

    def rep(m):
        if m.group(0) == " " or m.group(0) == "_":
            return "[ _]"
        elif m.group(0) == "(" or m.group(0) == ")" or m.group(0) == "*" or m.group(0) == "+" or m.group(0) == "=" or m.group(0) == "?" or m.group(0) == "!" or m.group(0) == "^" or m.group(0) == "-":
            return "\\" + m.group(0)

    return re.sub(r"[ _()*+=?!^-]", rep, s)

1 个答案:

答案 0 :(得分:0)

希望可以帮到您!

def rep(m):
    if m.group(0) in (" ","_"):
        return "[ _]"
    elif m.group(0) in ("(",")","*","+","=","?","!","^","-"):
        return "\\" + m.group(0)

return re.sub(r"[ _()*+=?!^-]", rep, s)