Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 寻找正则表达式的置换_Python_Regex_Recursion - Fatal编程技术网

Python 寻找正则表达式的置换

Python 寻找正则表达式的置换,python,regex,recursion,Python,Regex,Recursion,我试图找到一个正则表达式(s)的排列,它们也是正则表达式,这是我到目前为止得到的: def permutations(s): leaves = ['1', '2', '0', 'e'] possible = [] if len(s) == 3 and s[1] in leaves: return s elif len(s) > 3: for i in range(len(s)): current_ch

我试图找到一个正则表达式(
s
)的排列,它们也是正则表达式,这是我到目前为止得到的:

def permutations(s):
    leaves = ['1', '2', '0', 'e']
    possible = []

    if len(s) == 3 and s[1] in leaves:
        return s
    elif len(s) > 3:
        for i in range(len(s)):
            current_character = s[i]
            other_characters = s[:i] + s[i+1:]
            for possible_regexes in permutations(other_characters):
                if is_regex(possible_regexes):
                    possible = [[current_character] + [other_characters]]
        return possible

    for regexes in possible:
        print(regexes)
但是,我收到一个错误,告诉我不能在字符串上迭代,我很确定这就是问题所在:
可能=[[当前字符]+[其他字符]]
。我如何返回它,以便迭代每个排列,然后确定该排列是否有效

编辑这是错误
TypeError:“NoneType”对象不可编辑

EDIT2完全回溯:
文件“regex_functions.py”,第86行,在
打印(所有正则表达式置换(‘(1.2)’)
文件“regex_functions.py”,第68行,在所有的regex_排列中
对于所有\u正则表达式\u置换中可能的\u正则表达式(其他\u字符):
文件“regex_functions.py”,第68行,在所有的regex_排列中
对于所有\u正则表达式\u置换中可能的\u正则表达式(其他\u字符):

TypeError:“非类型”对象不可编辑

您发布的回溯引用的函数名与您发布的函数名不同,但它似乎刚刚重命名。我想情况就是这样

您应该关注的跟踪项是回溯中的最后一项

File "regex_functions.py", line 68, in all_regex_permutations for possible_regexes in all_regex_permutations(other_characters): TypeError: 'NoneType' object is not iterable 文件“regex_functions.py”,第68行,在所有的regex_排列中 对于所有\u正则表达式\u置换中可能的\u正则表达式(其他\u字符): TypeError:“非类型”对象不可编辑 该错误表示
NoneType
不可编辑。这意味着调用
所有正则置换(其他字符)
的结果在某个点返回
。您需要分析您的代码,找出它在什么时候实际返回
None

我暗示并不是每个代码路径都返回一个值,这是这个错误的最终原因(我看到了其他可能的错误,但这是一个不同的问题)。您应该问自己以下问题:“此函数的所有代码路径是什么?”以及“我是否为每个函数返回正确的结果?”

我强调了该函数中的代码路径

  • 橙色路径将返回
    s
    是什么。假设
    s
    是一个列表或其他期望的类型,那么这就行了
  • 绿色路径将返回任何可能的
    <代码>可能设置在两个不同的点上,即函数顶部的初始值和循环体中赋值的新结果
  • 蓝色路径将一直延伸到函数的末尾。在Python中,如果函数没有显式返回值,则理解为返回
    None

  • 蓝色路径是问题所在。你没有在那里归还任何东西。这需要纠正。

    不要说你有错误,要表现出来。这里尤其如此,因为您说错误消息告诉您不能迭代字符串,但可以迭代字符串。此外,您指向的行不进行任何迭代。请发布完整的回溯。@DSM抱歉,请检查我的编辑!不是每个代码路径都返回一个值。。。。做点什么吧。@JeffMercado我什么都试过了,我好像都不知道该怎么做,是吗?逐行检查代码。如果你有,你会发现问题的。