Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.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 程序是如何使用递归和for循环的?在下面的程序中,tail作为函数参数的必要性是什么?_Python - Fatal编程技术网

Python 程序是如何使用递归和for循环的?在下面的程序中,tail作为函数参数的必要性是什么?

Python 程序是如何使用递归和for循环的?在下面的程序中,tail作为函数参数的必要性是什么?,python,Python,考虑以下代码:程序是如何使用递归和for循环的?在下面的程序中,tail作为函数参数的需要是什么 def permutations(head, tail=''): if len(head) == 0: print(tail) else: for i in range(len(head)): permutations(head[0:i] + head[i+1:],tail+head[i]) permutations('a

考虑以下代码:程序是如何使用递归和for循环的?在下面的程序中,tail作为函数参数的需要是什么

def permutations(head, tail=''):
    if len(head) == 0: 
        print(tail)
    else:
        for i in range(len(head)):
             permutations(head[0:i] + head[i+1:],tail+head[i])

permutations('abcd')

此函数用于获取
str
字符的所有可能排列

递归部分负责从
中提取字符并将其放入
,当
头==''
时,它将打印

<>代码< < /Cord>循环】的作用是考虑所有的可能性。事实上,它首先从
头上取第一个字符,然后是第二个,然后是第三个,依此类推

通过结合这两部分,我们得到了所有的排列


希望这对您有所帮助。

谢谢!在函数内部再次调用permutation()时,我仍然无法将控制流可视化为recursionHi。你似乎有很多问题。你能分别问每个问题吗?