Recursive function dichotomy & dictionary listing ternary expressions with expressions formula generate anonymous function and its application


Recursive function #
# function directly or indirectly, and calls itself the call to stage

## to add
# import sys # Import system modules
# print (sys.getrecursionlimit ()) # # is not very accurate calculation of the maximum number of recursive
# sys.setrecursionlimit (2000 ) to modify the maximum number of recursions #
#
# DEF FUNC (n-): define a function parameter # n-
# Print ( 'from FUNC', n-) and variable print string # n-
# FUNC (n-+. 1) within itself a function call # themselves and pass parameter n-+ 1
# FUNC (1) # calling function, pass arguments = 1, this can calculate the number of recursions, calls itself, endless loop maximum recursion 998




# DEF index (): # define a function 1
# print ( 'from index') # print string
# Login () function call # 2
#
# Def login (): # 2 define a function
# print ( 'from login') # print string
# index () # call the function 1
# the Login () # execute infinite loop error, more than the maximum number of recursion, recursive will open a local name space, accounting for memory



recursion function should not be unlimited #


# recursive
"" "
recursively divided into two stages
1. back: the process is repeated again and again, repeating this process must be established in each repetition of the problem complexity should fall
until a final end condition
2. recursion: derivation process back again
"" "
" ""
Age (. 5) = Age (. 4) + 2
Age (. 4) = Age (. 3) + 2
Age (. 3) = Age (2) + 2
Age (2) = Age (. 1) + 2
Age (. 1) = 18 is

Age (n-) = Age (n--. 1) + 2 # n->. 1
Age (. 1 ) = 18 # n = 1 can be used as the termination condition of the cycle, n = 1 return return 18
"" "


#
# # Recursive function
# Def age (n): # solve the above problems with the recursive function
# if n == 1: # must have end condition if n is equal to 1 age (1) = 18 returns 18 is
# # 18 is returned return 18 is
# return age (n-1) + 2 # is not age (1) returns Age (n--. 1) + 2
# RES = Age (. 5) # Age (. 5) assigned to the RES
# Print (RES) # Print RES




# L = [1, [2, [3, [4, [5, [6, [7, [8, [9, [10, [11, [12, [13,]]]]]]]]]]] ]]
# the list of numbers in order to print out (the number of layers circulation is a point you have to consider)
# for i in L: derivation of this idea how many values you need to write many times if cycle
# if type (i) int IS:
# Print (i)
# the else:
# for i in Item:
# IF of the type (Item) IS int:
# Print (Item)
# the else:
# for J in Item:
IF type # (Item) int IS:
# Print (Item)
# the else:


# DEF index (): lines of code # placeholder recommended Pass
# # # Pass replace a first embodiment (recommended Pass)
# #. replace the second embodiment .. #
# index ()




# # recursive functions not only to consider the number of cycles required to grasp the end condition
# l = [1, [2 , [3, [4, [5, [6, [. 7, [. 8, [. 9, [10, [. 11, [12 is, [13 is,]]]]]]]]]]]]]
# DEF get_num (L): # define a function
# for i in l : # cycle values from the list
# if type (i) is int : # is taken out if the value of the integer
# print (i) # prints
# the else: # otherwise
# get_num (i) # to continue execution of the function this is not an integer and the data for the cycle continues to a value of the discharge is determined to continue
# get_num (l) # End-defined function, called directly



# # Circulating empty list value, the error will not fail to
# L1 = []
# for I in L1:
# Print (I)







# # algorithm: Solving the Problems efficient method
# l = [1,3, 5,12,57,89,101,123,146,167,179,189,345] # define a list
# num = #'re looking for from a list of 345 values
# for i in l: # cycle turn values from the list
# if num == i: # if we are to looking to take the value of the
# print ( 'find it') # prints found
## list again this method to find the value, efficiency is very low, we need to find a complete list of more cycles



# # dichotomy: container type the number in the sequence must have a size
## to obtain an intermediate of the table index value, the length of the list divided by two, rounded, compare this value with the target value are equal, sizes, because this dichotomy find the digital container is by size sorting
##, if more than the target value, from the middle to large index direction, the index continues to an intermediate value, then a comparison cycle, the cycle again until it finds a target
# l = [1,3,5,12,57 , 89,101,123,146,167,179,189,345] # Create a list
# Target_num = 666 # defined target
# def get_num (l, target_num) : # define search algorithm function
# if not l: # first determine a target value not in the list
# print ( 'you to pay this task is not afraid to do ') if not prompted #
# # does not return directly out of a function, not looking
obtaining an intermediate index list # #
# print (l) # print the entire list
# middle_index = len (l) // 2 # intermediate value index, 2 except using rounding list length
## determines the size middle_index target_num with corresponding numbers
# if target_num> l [middle_index] : determines the target value # is greater than the intermediate index value
## cut right half of the list
# Num_right = l [middle_index + 1 :] # If so, he took the index of a sub-list to the end, care regardless of the end, fail to have an intermediate value determination
## function recursively calling get_num
# get_num (num_right , target_num) # and the target number of sub-list parameter passing, continue to call the function calculating
# elif target_num <l [middle_index] : # if the target value is less than the index value of the intermediate value
## the left part of the list cut
# num_left = l [0: middle_index] # is less than it takes an intermediate value of the index to the beginning of the sub-lists, care regardless of the end, fail to have an intermediate value determination
## function recursively calling get_num
# get_num (num_left, target_num) # and and pass the target number of parameters sublist, continue to call the function computes
# else: # If the index value equal to the target value and the intermediate
# print ( 'find it', target_num) # i.e., hit target
# Get_num (l, target_num) # function is called by value target list and target












# DEF my_max (x, the y-):
# IF x> the y-:
# return x
# the else:
# return the y-
# "" "
# large when x when returns x when y big time returns y
# when a condition exists to do one thing, do not set up another thing
# "" "
# x = 99999
# y = 9,898,898
# RES x = iF x> y y the else
# Print (RES)
# # If following the if condition is established to return the front if the value or the return value of the back of the else
'' '
X = 99999
Y = 9,898,898
RES = X if X> Y the else Y
if following the if condition is satisfied return if otherwise the return value is the value of the front behind the else
'' '

"" "
Ternary expressions with a fixed expression
value of 1 if condition else value 2
condition is true value 1
condition does not hold a value of 2
scenarios only recommend a triplet of expressions of only two cases of possible,
"" "
X =. 1 #
# 2 = Y
# = m. 3
#. 4 = n-
# RES IF X = X> Y the else (m IF m> n-the else (...)) can not nest so that

application of a triplet of expressions # under scenario only recommend only two cases of possible
''
in the triple expression of scenarios only recommend only two cases of possible
'' '
# # case 1
# is_free the iNPUT = (' Please input is free (y / n-) >>>: ')
# = is_free' free 'if is_free ==' y 'else ' charge '
# Print (is_free)
# case # 2
# username = INPUT (' username >>>: ')
# RES = 'NB' if username == ' jason' else ' spam'
# Print (RES)














# L = [ 'Tank', 'Nick', 'Oscar', 'Sean'] # define a list
# l1 = [] # the definition of an empty list
# for name in l:Cycle value #
# l1.append ( '% s_sb'% name) # string concatenation cycle
# # L1.append (name + '_sb ') # inefficient use is not recommended, total memory
# print (l1) # print a new list


## listing formula
'' '
RES = ['% s_DSB 'name for name in% l] # l cycle values from the list, the name assigned to, and then to the front of the string concatenation
'' '

# l = [' tank_sb ',' nick_sb ',' oscar_sb ',' sean_sb ',' jason_NB ']
# RES = [ '% s_DSB'% name for name in L]
# Print (RES)
# # results [ 'tank_sb_DSB', 'nick_sb_DSB', 'oscar_sb_DSB', 'sean_sb_DSB', 'jason_NB_DSB']
#
# RES = [name for name in l if name.endswith ( ' _ sb')] # does not support the rear case coupled else
# Print (RES)
# result [ 'tank_sb', 'nick_sb' , 'oscar_sb', 'sean_sb'] No Jason

# # turn out first for circulating a list in which each element
## and 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

'' '
res = [name for name in l if name.endswith ( '_ sb')]
cycle l values from the list, the name assigned to,
to judge whether to satisfy the condition if
the condition string concatenation to the front, does not meet directly discard
'' '













# # dictionary formula
# l1 = [' name ', ' password ',' hobby '] # definition list. 1
# L2 = [' Jason ',' 123 ',' on DBJ ',' Egon '] # definition list 2
# D # = {} empty dictionary defined
# for i, j in enumerate ( l1): # l1 enumerated list of elements and to obtain a correspondence relationship index, the index starts from 0
# d [j] = l2 [ i] # with key and value dictionary definitions, key is withdrawn the first list element, value is the second index of the first list by a list of values
# print (d) # print result is { 'name': 'jason' , 'password': '123', 'Hobby': 'on DBJ'}

#
# L1 = [ 'Jason', '123', 'read']
# d = {i:j for i,j in enumerate(l1) if j != '123'}
# print(d)
''''''
'' '
D = {I:! J for I, J in the enumerate (L1) J = IF' 123 '}
# define value pairs i: j cycle values i, j, i from an enumerated list 1 index , j is the element list,
if j is not equal to 123, to the foregoing composition formed on the key dictionary, if equal 123, then discard
'' '


#' '' '' '
' ''
set formula
'' '
# RES = I I in range for {(10) IF I!. 4} =
# # set formula, the set of elements i, from the range 0-10 cycles value does not take 10, assigned to i, to form a set
# print (res)
# # print result is {0, 1, 2, 3, 5, 6, 7, 8, 9}
#

#
'' '
generator expression
' ''
# RES1 = (I for I in Range (10) IF I ! = 4) # write formula but not the tuples generator expression
## generator expressions, elements i, from the range 0-10 cycles value does not take 10, assigned to i, if i is not equal 4, to the front of the generator, not printed
# Print (RES1)
# # print result is <generator object <genexpr>at 0x000001C9444FEFC0> result generator memory address
#
# # cycle values from the generator, the print is a value of a
For i in res1 #:
# Print (i)








# anonymous function
"" "
did not name a function of

the characteristics of the anonymous function
temporarily run out there is gone
." ""
# DEF my_sum (the X-, the y-): # define a function, location parameter X, Y
# + Y # X return run function returns the sum of two parameters
#
'' '
the lambda anonymous function
res = (lambda x, y: x + y) (1,2)
directly assign the result to a function res = (anonymous function declaration, there are two parameters x, y: the operating parameters of the addition) transmission parameters (1,2)
#: shape function corresponds to the left colon reference parameter: function return value
#: right of the colon corresponds to the function's return value
# anonymous function usually not used alone, is used together with the built-in function
'' '
# RES = (the lambda X, Y: X + Y) (1,2)
# Print (RES )
# FUNC the lambda = X, Y: X + Y
# Print (FUNC (1,2)) # can also be used to undertake the function anonymous functions and parameter passing operation brackets

#: left equivalent function parameter
#: Right the function's return value is equivalent to
# anonymous function not normally used alone,It is used in conjunction with the built-in functions





L = # [1,2,3,4,5]
# Print (max (L)) is based on an internal # for selecting the maximum value to the cycle

"" "
the ASCII code table letter corresponds to a decimal number
# AZ 65 90
# AZ 97 122
Print (CHR (97)) # View ASCII code table 97 corresponds to which character output a
"" "
# Print (CHR (97)) # View ASCII code table 97 corresponds to which character output a

# # Comparative pay return names
# D = {
# 'Egon': 30000,
# 'Jason': 88,888,888,888,
# 'Nick': 3000,
# 'Tank': 1000
#} # dictionary definition
# def index (name): # defined function (position parameter name)
# return D [name] # returns the dictionary key value name of
#
# Print (max (D, key = the lambda name: D [name]))
#
'' '
printed (job large value function the result, (the goal is a dictionary d, d certainly is dictionary's key, that is, the names returned;
key is designated function, max will press the key to compare the size of the specified function, seeking maximum specified key is a lambda function anonymous,
anonymous functions need to pass a variable name from the target, which can only be taken from the dictionary to make key variable name value,
the back key assigned anonymous function returns the value of the variable name dictionary d corresponding to the value, i.e. max doing arithmetic, will be designated by the function key comparison, value i.e. Comparative dictionary)
max function returns the operation ends d is the value taken from the dictionary, i.e. only return key, i.e., key returns d dictionary specified function execution result of the arithmetic value corresponding key
'' '
# Print (min (d, = the lambda key name: d [ name]))
#
'' '
min () built-in function, and max () built-in function, empathy
' ''





# built-in functions to five
'' '
Map map
zip fastener
filter filtration
sorted sorting
reduce cumulative
' ''

# # Map mapping
# L = [1,2,3,4,5,6]
# Print (List ( 'Hello')) # result [ 'h', 'e' , 'l', 'l', 'o' ]
# Print (List (the Map (the lambda the X-:x + 5, l))) # for loop-based
# # Print a new list, the list will correspond to elements corresponding to a lambda anonymous way function, x values of variables, take the value x + 5, map the object l is a list
of all ## results in the list is to elements corresponding one by one +5 principle is based on the result of the for loop [. 6,. 7,. 8,. 9, 10,. 11]


# # # ZIP based fastener for loop
# L1 = [1,2,]
# L2 = [ 'Jason' 'Egon', 'Tank']
# L3 = [ 'A', 'B', 'C']
# Print (List (ZIP (L1, L2, L3)))
# # returns a value of [(1 ' Jason ',' A '), (2,' Egon ',' B ')]
' ''
ZIP cycle time value from each object, based on the values for loop,
then to take the same value in each object cycles out value as a tuple, multiple cycle multiple tuples in a list
similar to the plurality of containers together correspondence values, a corresponding sawtooth like fastener, if there is an insufficient number of values container object , to press on a small number of groups, not being given
as the fastener is not on the serrations is no longer pull
'' '



# L = [1,2,3,4,5,6]
# Print (List ( filter (lambda x:! x = 3, l))) # basis for loop
## as a result of [1, 2, 4, 5, 6]
'' '
Printing, using a list of filter method, according to the anonymous function lambda filter, taking loop variable x, for loop-based
Printing, sorting methods result, the object is a list L, comma designation method, is flashback, is not specified the default positive sequence decimal alphabetical order is alphabetical corresponding ASCII code table to compare the size, the smaller the value the more front letters , i.e., the smaller the character sort and distinguish the sorted: sort method is applied on the list, the sorted can be sorted objects operate all iterations. list sort method returns a list of the existing operation, no return value, and built-in functions sorted method returns a new list, rather than operation performed on the basis of the original. '' ' #
























The reduce # #
# Import from the reduce functools
# L = [1,2,3,4,5,6]
# Print (the reduce (the lambda X, Y: X + Y, L,. 19)) a first initial value #. 19 parameter
'' '
print, the results of the reduce method, define an anonymous function with parameters x, y specified method x + y, the object is to specify a list of values L, specify the initial value of 19
if initial value of 19, the calculation to take the first value plus 19, plus the second value then removed and the front, followed by cycle
if the initial value is not specified, the default initial value of the first element in the list, i.e., remove the first two elements are added, and then then remove the added third element and the first two, then take a value in cycles
' ''
# a case where the initial value does not exist according to the following rules
# to obtain the first two elements are added
each time after # obtaining a first result and then summed with the summed


















































# DEF Age (n-):
# Age (. 5) = 18 is
# Age (n-+. 1) Age = (n-) + 2
# return Age
# Age (. 1)


# List1 = [2,3,1,5]
# len (List1)

# DEF FUNC ():
# List [len (the list.sort ()) // 2]> n-:
# FUNC ()

# 123 = target_num
# def get_num(l,target_num):
# mid_num = len(list1) // 2
# if mid_num > target_num :
# l_left = list1[0:mid_num]
# elif mid_num < target_num :
# l_right = [mid_num+1:]
# else:
# print(target_num)












Guess you like

Origin www.cnblogs.com/xiaozhenpy/p/11220564.html