[Python] Understand how to use recursive functions

[Python] Understand how to use recursive functions

Recursive functions can be used when the depth of the nested structure is uncertain. (When handling JSON file data, etc.)

What is a recursive function?

To call yourself (function) in a function. └ Literally "recursive", return to yourself again.

Precautions for recursive functions

--A termination condition is required. --If you don't, you will end up in an infinite loop. --The default limit is 1000 times.


Find the sum up to the specified integer.


def sum(n):
    #Exit conditions
    if n <= 0:
        return n
    #Call yourself(Recursive function)
    return n + sum(n-1)

If n = 4, add the numbers 4,3,2,1 in order.

Function execution result



Processing content

If you enter an integer greater than or equal to 0

(1) The first thing to be executed is n + sum (n-1) (2) After entering a numerical value in n, perform sum (n-1). └ The argument becomes "n-1". (3) If n-1 is greater than 0, repeat n + sum (n-1).

Extract an integer from a multidimensional array.

Data can be obtained from a multidimensional array by combining if statements, for statements, and recursive functions.


data0 = [1,[2]]

def number(arr):
    #Prepare a list to put the result
    #If it is an integer, add it to result
    if isinstance(arr, int):
    #Processing in case of list type
    if isinstance(arr, list):
        for brr in arr:
            #Recursive function. Execution result(result)To add
            result += number(brr)
    #Function return value
    return result


[1, 2]

Set a return value (return). * Be careful of the location

▼ Since there is no specified depth, you can go in any number of dimensions. * Default is up to 1,000 times


data1 = [1,[2],[[3]],[[[[4]]]],[[[[5,6,7],8,9]]],10]

def number(arr):
    if isinstance(arr, int):
    if isinstance(arr, list):
        for brr in arr:
            result += number(brr)
    return result


[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]       

Recommended Posts

[Python] Understand how to use recursive functions
python3: How to use bottle (2)
Understand how to use django-filter
[Python] How to use list 1
Python: How to use pydub
[Python] How to use checkio
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
Python: How to use async with
[Python] How to use Pandas Series
How to use Mysql in python
How to use OpenPose's Python API
How to use ChemSpider in Python
How to use FTP with Python
How to use PubChem in Python
How to use python zip function
[Python] How to use Typetalk API
[Python] Summary of how to use split and join functions
Comparison of how to use higher-order functions in Python 2 and 3
[Python] Summary of how to use pandas
[Introduction to Python] How to use class in Python?
How to install and use pandas_datareader [Python]
[python] How to use __command__, function explanation
[Python] How to use import sys sys.argv
Memorandum on how to use gremlin python
[Python2.7] Summary of how to use unittest
python: How to use locals () and globals ()
How to use __slots__ in Python class
How to use "deque" for Python data
How to use Python zip and enumerate
Summary of how to use Python list
How to use regular expressions in Python
[Python2.7] Summary of how to use subprocess
How to use is and == in Python
[Blender x Python] How to use modifiers
[Question] How to use plot_surface of python
How to use functions in separate files Perl and Python versions
Basic grammar of Python3 system (how to use functions, closures, lambda functions)
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to install Python
How to use Pandas 2
How to use Virtualenv
How to use pytest_report_header
How to install python
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables