【问题标题】:Recursive function in Python returns only the last list valuePython中的递归函数仅返回最后一个列表值
【发布时间】:2021-12-31 22:17:39
【问题描述】:

我对编程比较陌生,对 Python 完全陌生。 我目前正在练习递归函数。

我试图实现一个简单的排序函数,它递归地搜索给定列表的最小值并将其附加到新列表中。

但是,当返回列表时,它只返回列表的最后一个(最高)值。
最让我困惑的是,如果我在返回结果之前放一个print(result),该函数会打印整个排序列表。

这是我的代码。
PS:函数smallesListIndex() 搜索给定列表的最小值的索引。 功能:

def recSort(liste, result=None):
    listIn = liste
    length = len(liste)
    smallestIndex = smallestListIndex(listIn)
    
    # To prevent creating a new list in every recursion, an intermediate list is to be created.
    if result is None:
        result = []
        
    if length == 1:
        result.append(listIn[smallestIndex])
        print(result)
        return result
        
    else:
        result.append(listIn[smallestIndex])
        listIn.pop(smallestIndex)      
        length -= 1
        # the intermediate list is to be given to the next recursion.
        return recSort(listIn, result)

主要:

if __name__ == "__main__":
    liste = [5, 2, 4, 8, 7, 10, 6]
    recSort(liste)
    print(liste)

输出:

[2, 4, 5, 6, 7, 8, 10] #  Output from the print function
[10] # Output from the main function

【问题讨论】:

  • 你返回了排序后的列表,但你从来没有对它做任何事情liste = recSort(liste)
  • 哦,原来如此!比我想象的要容易,但这已经困扰了我很长一段时间了!!谢谢

标签: python sorting recursion


【解决方案1】:

您的函数recSort 返回一个列表,在您的示例中,您没有使用它。

试一试,看看结果:

if __name__ == "__main__":
    liste = [5, 2, 4, 8, 7, 10, 6]
    res = recSort(liste)
    print(res)

为了解释,您的函数修改了输入列表。这就是为什么你只能得到一个值。

如果您不想修改输入,可以在函数中尝试listIn = liste[:]

【讨论】:

    【解决方案2】:

    如果您使用print(recSort(liste)) 而不是print(liste),那么您的代码可以工作。

    下面的完整更正代码,还添加了我对smallestListIndex() 的简单实现,以实现完整的可运行示例,并重新格式化代码。

    Try it online!

    def smallestListIndex(l):
        return l.index(min(l))
    
    def recSort(liste, result=None):
        listIn = liste
        length = len(liste)
        smallestIndex = smallestListIndex(listIn)
    
        # To prevent creating a new list in every recursion, an intermediate list is to be created.
        if result is None:
            result = []
            
        if length == 1:
            result.append(listIn[smallestIndex])
            #print(result)
            return result
            
        else:
            result.append(listIn[smallestIndex])
            listIn.pop(smallestIndex)      
            length -= 1
            # the intermediate list is to be given to the next recursion.
            return recSort(listIn, result)
    
    if __name__ == "__main__":
         liste = [5, 2, 4, 8, 7, 10, 6]
         print(recSort(liste))
    

    输出:

    [2, 4, 5, 6, 7, 8, 10]
    

    【讨论】:

      【解决方案3】:
      1. 您的列表 liste 不一样,因为不使用 copy 函数,您只是在创建引用,所以无论您对引用做什么,都对原始列表执行此操作

      2. 您正在从 recSort 函数返回内容,但您没有将它们保存在任何地方。您可以在 main 中使用类似 result = recSort(liste) 的内容,然后使用 print(result)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-22
        • 2011-02-04
        • 2014-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-13
        相关资源
        最近更新 更多