繁体   English   中英

递归遍历列表并与值进行比较(python)

[英]Recursively going through list and compare with a value (python)

任务是通过一个列表对go,将其与一个值进行比较,然后返回该值出现的次数。 比较有效,当我找到匹配值时,我想将其保存在列表中。 然后我想得到那个列表的长度。 问题是每次我再次调用 function 时,列表都会重置,我该如何避免呢?

    def count_occurences(values, value):
        matched_values = []
        if values:
            if values[0] == value:
                matched_values.append(values[0])
                count_occurences(values[1:], value)
            else:
                count_occurences(values[1:], value)
        else:
            return 0
        return len(matched_values)
    count_occurences([1, "one", 2, "two", "two", 3, "three", "three", "three"], "two")

在这种情况下,预期的 output 将是:2。

正如@juanpa.arrivillaga 在评论中所说,您正在将matched_values初始化为每次调用都清空列表,因此您没有有效地返回任何内容。

脚本可以简化,例如:

def count_occurences(values, value):
    if values:
        return (values[0] == value) + count_occurences(values[1:], value)
    return 0

n = count_occurences([1, "one", 2, "two", "two", 3, "three", "three", "three"], "two")
print(n)

印刷:

2

您可以通过简单地将matched_values分配移到function 之外来解决您的直接问题,如下所示。

matched_values = []

def count_occurences(values, value):
    if values:
        if values[0] == value:
            matched_values.append(values[0])
            count_occurences(values[1:], value)
        else:
            count_occurences(values[1:], value)
    else:
        return 0
    return len(matched_values)
count_occurences([1, "one", 2, "two", "two", 3, "three", "three", "three"], "two")

然而,这段代码确实提出了比它回答的问题更多的问题。 大概您将其作为学习练习进行,因为您无需特殊的 function 或递归即可轻松完成此操作。 所以考虑到这一点,我会问你:

  • matched_values的意义何在? 根据定义,它只是重复的同一个条目,因此它不会添加任何信息而不是只知道匹配的数量
  • 递归是函数式编程中的一种常见模式,但您的函数的主要目的是改变 function 之外的变量 - 这被称为副作用,通常被认为在函数式风格中是不受欢迎的

考虑到这些,您可能会考虑这样的事情,它消除了列表并且不涉及突变和副作用。

def count_occurences(values, value):
    if values:
        if values[0] == value:
            return 1 + count_occurences(values[1:], value)
        else:
            return count_occurences(values[1:], value)
    else:
        return 0

(这与上面 Andrej Kesely 建议的简化基本相同)

 matched_values = [] 

 def count_occurences(values, value, matched_values):
            if values:
                if values[0] == value:
                    matched_values.append(values[0])
                    count_occurences(values[1:], value, matched_values)
                else:
                    count_occurences(values[1:], value, matched_values)
            else:
                return 0
            return len(matched_values, matched_values)

 count_occurences([1, "one", 2, "two", "two", 3, "three", "three", "three"], "two", matched_values)

每次调用递归 function count_occurences时都会重置该列表,因为您在 function 的开头使用以下行重新创建了该列表: matched_values = [] 这将创建一个新变量matched_values ,它指向memory 中的新位置,并且无法访问上一轮找到的内容。

在调用 function 之前初始化此空列表可确保每次 function 运行时都会访问 memory 中的相同位置。 每个递归调用都将在同一个列表上运行,并且您不会丢失信息。

暂无
暂无

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

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