简体   繁体   English

递归与迭代函数 Python

[英]Recursive vs Iterative Functions Python

I am currently learning Python and would like some clarification on the difference between iterative and recursive functions.我目前正在学习 Python 并且想澄清一下迭代函数和递归函数之间的区别。 I understand that recursive functions call themselves but I am not exactly sure how to define an iterative function.我知道递归函数会调用自己,但我不确定如何定义迭代 function。

For instance, I wrote this code例如,我写了这段代码

random_list = ['6', 'hello', '10', 'find', '7']

def sum_digits(string):
    return sum(int(x) for x in string if x.isdigit())

print "Digits:", sum_digits(random_list)

I thought that this was an iterative function but after doing some research I am not sure.我认为这是一个迭代 function 但在做了一些研究后我不确定。 I need to know specifically because the next exercise asks me to write a version of the function that is recursive/iterative (depending on what my first function is).我需要具体了解,因为下一个练习要求我编写递归/迭代的 function 版本(取决于我的第一个 function 是什么)。

so the question is "write an iterative and recursive version of sum". 所以问题是“编写sum的迭代和递归版本”。 great. 大。

don't use the built-in sum method, and write your own. 不要使用内置的sum方法,而是自己编写。 i'll give you the iterative, you should figure out the recursive: 我给你迭代,你应该弄清楚递归:

def my_iterative_sum(a):
    res = 0
    for n in a:
        res += a
    return res

this is iterative because it iterates over all the values and sums them up. 这是迭代的,因为它会迭代所有值并将其求和。

edit: clearly your post is iterative. 编辑:显然,您的帖子是迭代的。 are you calling function f from within function f ? 你调用函数f来自于函数f no. 没有。

maybe reading up on what recursion is will help with this. 也许阅读一下什么是递归将对此有所帮助。 https://www.google.com/search?q=recursion https://www.google.com/search?q=recursion

递归函数在未达到出点时调用自身,而迭代函数通过范围内的迭代来更新计算值。

To those who might still want to see the difference between recursive and iterative function. 对于那些可能仍然希望了解递归和迭代函数之间的区别的人。

iterative 反复的

def iterative_sum(n):
    result = 1
    for i in range(2,n+1):
        result *= i
    return result
print(iterative_sum(5))

iteration is when a loop repeatedly executes until the controlling condition becomes false 迭代是循环重复执行直到控制条件变为假

recursive 递归的

def recursive_sum(n):
    if n == 1:
        return 1
    else:
        return n * recursive_sum(n-1)
print(recursive_sum(5))

recursive function is when a function calls itself 递归函数是函数调用自身时

This link explains it much better https://techdifferences.com/difference-between-recursion-and-iteration-2.html 该链接对其进行了更好的说明https://techdifferences.com/difference-between-recursion-and-iteration-2.html

In case you came here looking for recursive functions in Python, here is an example of a recursive sum如果你来这里寻找 Python 中的递归函数,这里有一个递归求和的例子

from typing import Optional

>>> def r_sum(v: int, sum: Optional[int] = None) -> int:
...   if sum is None:
...     sum = 0
...   if v == 0:
...     return sum
...   else:
...     sum = sum + v
...     prev = v - 1
...     return r_sum(prev, sum)
... 
>>> r_sum(3)
6
>>> r_sum(4)
10
>>> r_sum(5)
15

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

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