day, 12 unknown content

Content Today

  • recursive function

  • dichotomy

  • A triplet of expressions

  • List of Formula

  • Dictionary of formula

  • Anonymous function

  • Commonly used built-in functions

A recursive function

1. What is a recursive function

The so-called recursive function is a function call to the function in its interior, repeated calls continue until the function execution condition is not satisfied to stop calling and outputs the result

2, two-stage recursive function

Performing recursive function can be divided into two phases, respectively, and backtracking recursion.

Back: that is repeated again and again of course, this repetitive process must be based on the complexity of each iteration problems are reduced until there is a final end conditions.

Recursive: again and again to push back the process.

3, the recursive function is used

When using a recursive function does not need to consider the number of cycles, only we need to grasp the condition of the end of the can.

If the condition is not over recursive function, will always run, python internal limit up to 1000 times, can be modified by calling the recursive layers os module. Too many layers of recursion is not very friendly to memory, not recommended to modify the restriction.

DEF Age (n-):
     IF n-==. 1:   # must have end condition. 1 = n- 
        return 18 is
     return Age (. 1-n-) 2 + 
RES = Age (. 5 )
 Print (RES)   # 26 is
L = [. 1, [2, [. 3, [. 4, [. 5, [. 6, [. 7, [. 8, [. 9, [10, [. 11, [12 is, [13 is ,]]]]]]]]] ]]]]
 DEF get_num (l):
     for I in l:
         IF type (I) iS int:
             Print (I)
         the else : 
            get_num (I) 

get_num (l)   # loop to print all the numbers l

Second, the algorithm of dichotomy

Algorithm: algorithm that is not yet solve the problem of efficient methods

Dichotomy: Find the numbers you want to find a good number have been arranged in a container the size of the order

= 666 target_num 
L = [1,3,5,12,57,89,101,123,146,167,179,189,345 ]
 DEF get_num (L, target_num):
     IF  not L:
         Print ( ' you to pay this task is not afraid to do ' )
         return 
    # get a list intermediate index 
    Print (L) 
    middle_index = len (L) // 2
     # determines target_num middle_index with a size corresponding to the digital 
    IF target_num> L [middle_index]:
         # cut right half of the list 
        num_right = L [middle_index +. 1 :]
         # then recursive call get_num function 
        get_num (num_right, target_num)
    elif target_num < L [middle_index]:
         # cut listing left part 
        num_left = L [0: middle_index]
         # recursively call get_num function 
        get_num (num_left, target_num)
     the else :
         Print ( ' Find IT ' , target_num) 

get_num (L, target_num )

Third, a triplet of expressions

Ternary expressions with: a flow control code written in the form of expression by receiving the return value of a variable name.

99999 = X 
Y = 9,898,898 
RES = X if X> Y the else Y 

# If the latter condition is satisfied if the return to the previous value if the following values are otherwise the else 
Print (RES)   # 9,898,898

Expression fixed triples expression is:

value res = 1 IF Condition else the value of 2 (the value of a condition is established condition is not established value 2)

Ternary expressions with multiple layers may also be nested determination:

res = x if x > y else (m if m >n else (...))

The value is determined as the value of a single layer.

Fourth, the list of formula

List formula: the value of a satisfies the condition in the list, generates a new list.

Basic forms: res = [meet added to the list of elements for the elements added to the list if the data type in the type of vessel conditions determination]

= L [ ' tank_sb ' , ' nick_sb ' , ' oscar_sb ' , 

RES = [name for name in L IF name.endswith ( ' _sb ' )]   # latter case does not support the combined else 
# to remove list for sequentially circulating inside each element 
# then handed over to determine if conditions are met will be handed over for the previous code 
# If the condition does not hold direct current element abandon 

Print (RES)   # [ 'tank_sb', 'nick_sb', 'oscar_sb', 'sean_sb' ]

V. Dictionary of formula

Dictionary expression: the composition meets the conditions in key-value pairs in a dictionary, to generate a new list.

Basic forms: fundamental form: res = {health value of the element satisfies the condition of the composition for i, j in the data type if the type of vessel conditions determination}

l1 = ['name','password','hobby']
l2 = ['jason','123','DBJ','egon']

d = {j:l2[i] for i,j in enumerate(l1)}
print(d)  # {'name': 'jason', 'password': '123', 'hobby': 'DBJ'}

Six, anonymous functions

Popular speak anonymous function is to function without a name, with the tune with the use, run out to destroy the function of a temporary existence.

res = ( lambda x, y: x + y) (1.2 )
 print (res)   # 3

Generally used with anonymous functions and built-in functions

Seven commonly used built-in functions

Commonly used by a number of built-in functions, including print, input and so on. Today, mainly about the map, zip, filter, sorted, reduce.

# Map ZIP the reduce the sorted filter 

# Map map 
L = [1,2,3,4,5,6 ]
 # Print (List ( 'Hello')) 
Print (List (Map ( the lambda X: X +. 5, L)) )   # [6, 7, 8, 9, 10, 11] for loop based on 


# ZIP basis for loop fastener # 
L1 = [1,2 ,] 
L2 = [ ' Jason ' , ' Egon ' , ' Tank ' ] 
L3 = [ ' A ' , ' B ' , ' C ' ]
 Print(List (zip (L1, L2, L3)))   # [(. 1, 'Jason', 'A'), (2, 'Egon', 'B')] 
# the number of types of container elements when the zip are not the same, the number of elements obtained as a result of the minimal number of elements in a container of the type of data element 


L = [1,2,3,4,5,6 ]
 Print (List (filter ( the lambda X: X =! . 3, L)))   # [. 1, 2,. 4,. 5,. 6] based cycle for 
# filtered off condition is not satisfied in accordance with the conditions of the element, the element returns to meet the conditions 


L = [ ' Jason ' , ' Egon ' , ' Nick ' , ' Tank ' ]
 Print (the sorted (L, Reverse = True))   # [' Tank ',' Nick ',' Jason ','There '] 
#When l is a string of elements when comparing the first character in each string ascii code value 
# when l elements are digital, according to the size of the digital compare 
# when reverse is True when the left right, from small to large 
# when the value False to reverse from left to right, in descending 


from functools Import the reduce 
L = [1,2,3,4,5,6 ]
 Print (the reduce ( the lambda X, Y: X + Y, L,. 19))   # 40 
# . 19 initial values, each taking a parameter do adder, and calculates the number of all 
# a case where the initial value does not exist according to the following rule 
# first start Get two sum elements 
# each time the acquisition result and a time after the addition and then added

 

Guess you like

Origin www.cnblogs.com/le-le666/p/11184342.html