In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the example analysis of recursive functions in Python3. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Simple example of recursive function of Python3
Overview
A recursive function calls its own function directly or indirectly, and there must be a clear recursive end condition in the recursive process, which is called recursive exit. Recursion is extremely powerful in being able to traverse the structure of arbitrary, unpredictable programs, such as traversing complex nested lists.
Recursive summation
We can use recursive functions to implement a recursive version of the Python built-in function sum ().
# Recursive def d_sum (L): if not L: return 0 else: return L [0] + d_sum (L [1:]) sum_l = d_sum (range (10)) print (sum_l)
Sample result
forty-five
How does the recursive function add list elements? We know that functions have a local scope, and when each function call is opened, there is a copy of its own local scope on the runtime call stack, that is, L is different at each level. for example, we can show each level L more visually by adding a print statement for each call.
# Recursive def d_sum (L): # print this level L print (L) if not L: return 0 else: return L [0] + d_sum (L [1:]) # build a list of 0-10 numeric elements L = [i for i in range (10)] sum_l = d_sum (L) print (sum_l) [0,1,2,3,4,5,6,7,8,9] [1,2,4,5,6,7,8] 9] [2, 3, 4, 5, 6, 7, 8, 9] [3, 4, 5, 6, 7, 8, 9] [4, 5, 6, 7, 8, 9] [5, 6, 7, 8, 9] [6, 7, 8, 9] [7, 8, 9] [8, 9] [9] [] 45
Dealing with any structure
For example, we can use recursion to calculate the sum of all numbers in a nested sublist structure.
Def dd_sum (L): tot = 0 for x in L: if not isinstance (x, list): tot + = x else: tot + = dd_sum (x) return tot# nested list L = [1, [2 return tot# 3], [4, [5 peerre 6 print 7], 8], 9] sum_l = dd_sum (L) print (sum_l)
Example result:
forty-five
This approach seems complex, and we may use loop statements instead, but using recursive function logic is simple and clear, which is a big advantage of using it. This is the end of the sample analysis of recursive functions in Python3. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.