简体   繁体   English

Python中递归函数的说明

[英]Explanation of Recursive Function in Python

I am new to Python and trying to wrap my head around recursive functions. 我是Python的新手,正在尝试将我的头放在递归函数上。 I am understanding the overall concept but I came across an example that I can't seem to understand fully what it is doing. 我正在理解总体概念,但是遇到了一个例子,我似乎无法完全理解它在做什么。 A step by step breakdown of what is happening would be ideal, appreciate the help in advance. 逐步分解正在发生的事情将是理想的,请提前了解帮助。

def anagrams(s):
    if s == '':    
        return [s]
    else:
        ans = []
        for w in anagrams(s[1:]): 
            for pos in range(len(w)+1):
                ans.append(w[:pos] + s[0] + w[pos:])
    return ans  
def anagrams(s):
    # if argument <s> is a blank String
    if s == '':
        # return a list is just <s> in it
        return [s]
    else:
        # create a List
        ans = []
        # for each String character in calling this function recursively,
        # but removing character 0!
        for w in anagrams(s[1:]): 
            # for character position number starting at 0 and ending at
            # the length of the returned value of the recursed function
            # plus 1
            for pos in range(len(w)+1):
                # append a string with the following concatenation:
                # - the returned value's string from position 0 to position <pos>
                # - the character at position 0 of <s>
                # - the returned value's string from position <pos> to the end
                ans.append(w[:pos] + s[0] + w[pos:])
    # return the list
    return ans 
if s == '':    
        return [s]

If it is a blank string, there are no other anagrams, return a list with only that blank string.. 如果它是空字符串,则没有其他字谜,请返回仅包含该空字符串的列表。

Otherwise, 除此以外,

for w in anagrams(s[1:]):

separate s to the first character ( s[0] ) and the substring of all other characters ( s[1:] ). s分隔为第一个字符( s[0] )和所有其他字符的子字符串( s[1:] )。 Call the function again to find all anagrams of the substring (those are the w s), 再次调用该函数以查找子字符串的所有字词(都是w ),

for pos in range(len(w)+1):
        ans.append(w[:pos] + s[0] + w[pos:])

then for each of those, insert the first character of s in any possible position ( pos ) within w . 然后针对其中每一个,将s的第一个字符插入w内的任何可能位置( pos )。

Here is your function with a little print statement that helps to see whats going on. 这是您的函数,带有一些打印语句,可帮助您了解发生了什么。

def anagrams(s):
    if s == '':    
        return [s]
    else:
        ans = []
        level = len(s)
        for w in anagrams(s[1:]):
            print('level %d, s[0]: %s, w: %s' % (level, s[0], w))
            for pos in range(len(w)+1): 
                ans.append(w[:pos] + s[0] + w[pos:])
    return ans 

try: 尝试:

1. 1。

anagrams('a')

output: 输出:

level 1, s[0]: a, w: 

2. 2。

anagrams('ba')

output: 输出:

level 1, s[0]: a, w: 
level 2, s[0]: b, w: a

3. 3。

anagrams('cba')

level 1, s[0]: a, w: 
level 2, s[0]: b, w: a
level 3, s[0]: c, w: ba
level 3, s[0]: c, w: ab

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM