A recursive function
Recursion (recursion): Inside the function calls itself.
Recursion is divided into two stages: backtracking and recursion.
Back: back once, the complexity of the issue will be further reduced, until the termination condition is satisfied, the end.
Recursive: the termination condition is established and deduced back again and again
l = [1,[2,[3,[4,[5,[6,[7,[8,[9,[10,[11,[12,[13,]]]]]]]]]]]]] def get_num(l): for i in l: if type(i) is int: print(i) else: get_num(i) get_num(l)
import sys module pycharm introduced by sys.getrecursionlimit () method, the default value is known recursed 1000, but in fact only 997 (starting from 0).
() Method, the specified number of layers through recursive sys.setrecursionlimit.
Instead of code blocks in two ways:
The first way to replace pass (recommended pass)
second replacement way ...
II. Dichotomy
Scenario: Finding a value in an ordered container
Find method: Find a time are beginning to compare the middle.
l = [1,2,3,4,5,6,7,8,9,10] count =0 target= 5 def find_two (li,target) : global count print(count) print(li) count += 1 print(len(li)//2) middle = len(li) //2 if target < li[middle] : left = li [0:middle] find_two(left,target) elif target < li[middle] : right = li [middle+1:] print(right) find_two(right, target) elif li[middle] == target: print("find",count) find_two(l,target)
Three ternary expression
a if a > b else b
If the condition is met if the latter return to the previous value, otherwise the return value back else
Scenario: In only two cases of the condition
IV. Formula
1. list of Formula
Format: [variable name for the variable name in the determination condition if the type of container]
l = ['tank','nick','oscar','sean'] res = ["%s_rich" %i for i in l if i.endswith('k')] print(res)
2. Dictionary of formula
Format: {key: value for key: value in the type of container if the determination condition}
l1 = ['jason','18','dbj'] res= {i:j for i,j in enumerate(l1) if j != '18'} print(res)
3. collection formula
Format: {variable name for the variable names in the type of container if the determination condition}
print({i for i in range(10) if i != 9})
4. generator expression
Format :( variable name for the variable names in the container if the type judgment condition)
V. anonymous functions Introduction
Anonymous function: no function name can only be used once temporarily, run out of addresses on the destruction.
lamba function parameters: the function return value
max(dic,key=lambda k:dic[k])
ps: anonymous function with other functions normally use.
VI. Common built-in functions
map (func, * iterables) mapping function
= L [1,2,3,4,5 ] Print (List (Map ( the lambda Y: 10 Y +, L))) # container type of each element in front of the function call, and the return value to generate a new list.
ZIP ([ Iterable , ...]) compression function
= L1 [1,2 ,] L2 = [ ' Jason ' , ' Egon ' , ' Tank ' ] L3 = [ ' A ' , ' B ' , ' C ' ] Zipper = List (ZIP (L1, L2, L3) ) # can be more corresponding elements of the container types, stored in the form of tuples in the list. The return value is the address # length of the list depends on the type of container the shortest. List can be used to convert the object into a list of zip Print (Zipper) Print (list (zip (* Zipper))) # * may be used to reverse the process zip
filter ( function , Iterable ) filter function
= L [1,2,3,4,5,6 ] Print (List (filter ( the lambda Y:! Y = 10 , L))) # filter out unwanted elements by condition, and then generates a new list
sorted(iterable, reverse = bool ):
= L [1,2,3,4,5,6 ] Print (the sorted (L, Reverse = True)) # DESC Print (the sorted (L, Reverse = False)) # ascending
reduce(function, iterable, initia=None)
Use reduce function required from functools import reduce import function
from functools Import the reduce L = [1,2,3,4,5,6 ] Print (the reduce ( the lambda X, Y: X + Y, L,. 19 )) # anonymous function to obtain the value of the first two containers, if non-empty, then put the container on the initial value of the first container # then removed by a value, and with the addition, until all get; he listing plus the default values.