Python trip on the eighth day (anonymous function, built-in functions, map, filter, reduce)

Today's content is really a lot, have to say, to hear so cool, I have tried it:

# About anonymous functions, keywords for the lambda 
# The first is to look at before did not use an anonymous function of time if this is a simple
# DEF the Test (the X-):
# return the X-+ 1
# Print (the Test (10))

# then use lambda anonymous manner substantially writing mode function: lambda parameter: calculation method (returns the result of the calculation method, such as return back stuff)
# Test = lambda x: relationship # x + 1 where x and x + 1 is a fixed lambda, calculates x + 1 x incoming i.e. after
action # #test received anonymous function is to lambda, anonymous function because no name, call convenient, the use of variable reception
# print (test (10)) # here to begin operation directly

demonstrate #lambda, multi-use combined with other functions, anonymous functions can not have a very complicated operation
# the Test the lambda = the X-: the X-+ '_sb'
# Print ( 'operating results are anonymous function: ', test (' alex ') ) # ----> output:' alex_sb '

#lambda plurality of input parameters, return values of the plurality of writing lambda x, y, z: (x + 1, y + 1, z + 1) where bracketed to remember
# Test the lambda = X, Y, Z: (X + Y + Z, XYZ)
# Print (Test (7,9,6)) return #return Results will help add a tuple brackets, lambda need to add their own

methodology # programming: process-oriented, functional, object-oriented
#-Oriented process: find the entrance to solve the problem, according to a fixed process simulation to solve the problem

# functional programming: functional = programming language defined functions + mathematical sense function
# popular talk, functional is the programming language used to implement mathematical functions. Within this function object is immutable, Yaomei parameter is a function of
#, or return value is a function, not for and while loops, all by recursive loop to achieve, no variable assignment (ie no variable holds shape
# state, no assignment that is not changing (python functional language in the strict sense)

# DEF foo (the n-):
# Print (the n-)
#
# DEF bar (name): # is not a higher-order function (do not know what to say so much this is Gansha)
# Print ( '% S My name iS' name%)
#
# foo (bar) bar output of the memory address #
# foo (bar ( 'name' )) #bar plus () parenthesis, i.e., is a function performed bar
# #foo (bar ( 'name' )) of the calculation result, first output bar (Print statement) in the
## subsequent bar () does not return a value, run foo (), passing as a parameter None , the result is printed out None
# # name iS name calculation result My
# None

return value # which contain functions
# DEF bar ():
# Print ( 'bar from')
#
# DEF foo ():
Print # ( 'from foo')
# # return back bar here bar does not matter whether there has brackets
#
# n = foo () # assign a function to n, where n represents it foo ()
# n () This # at equivalent to foo () ()
# # operation result: from foo bar followed from

# DEF handle ():
# Print ( 'from handle')
# # handle return here if the bracket directly to run again, there is no brackets not continue to run
# h = handle () # handle output from
# H () # output from handle

# order function: a function of receiving a function name parameter is included in the function returns the value 2.

# recursion, when the next recursion level budget need to retain the current state of the layer, depending upon the next level function operation, into the lower layer
after #, the current layer if there is a code completion operations, if there is not this layer operation is required to retain operating state of a layer, the better to
# optimal recursive, it is necessary to optimize tail recursion

# tail call optimization, the final step of the function call, the next level
optimization exemplary tail recursion #

# optimization tail recursion
# tail non-recursive
# CAL DEF (SEQ):
# Le IF n-(SEQ). 1 ==:
# return SEQ [0]
# Head, * tail = SEQ
# return head + CAL (tail)
# Print (CAL (Range (100)))

# tail recursion (optimized)
# DEF CAL (L):
# Print (L)
# IF len (L ) ==. 1:
# return. 1 [0]
# First, SECOND, * args. 1 =
# L [0] + = First SECOND
# l.pop (L)
# return CAL (L)
#
# CAL X = ([I I in Range for (10)])
# Print (X)


# map function

# demand made in the num_l = [1,2,10,3,7] squares find out the value of each element
# original function mode wording (each squared request, increment, decrement)
# I first complex error examples
# num_l = [1,2,10,3,7]
# Test DEF (X): squaring method #
# num = []
for i in num_l #:
# num.append (i ** 2)
# return NUM
#
# Def test_1 (x): # is incremented by one method
# NUM = []
# for I in num_l:
# num.append (I + 1)
# return NUM
#
# Print (Test (num_l))
# Print (TEST_1 ( num_l))

## for the first optimization
## first defines a general method of catalog
# num_l = [5,6,7,8,9,10]
#
# DEF map_test (FUNC, Array):
# RES = []
for I in Array #:
# A = FUNC (I)
# res.append (A)
# return RES
#
# # Write local approach
# def zizeng (x): # self-energizing method
# return. 1 + X
# DEF Zijian (X ): # from the Save method
# X-return. 1
# DEF pingfang (X): calculates the square value #
# X ** 2 return
#
# # Print (map_test (zizeng, num_l))
# # Print (map_test (Zijian, num_l))
# # print (map_test (pingfang, num_l))
# #
# # # again to optimize the output statements, where the use of anonymous functions lambd explained calculation method
# # print (map_test ( X the lambda: X +. 1, num_l))
# # Print (map_test (the lambda X: X -. 1, num_l))
# # Print (map_test (the lambda X: X ** 2, num_l))
#
# # final ultimate the method, Python own internal map function, all the functions described above can be achieved all the operations
## i.e., direct output:
# Print (List (map (the lambda X: X +. 1, num_l))) # for each sequence in order to achieve elements increment
# print (list (map (zizeng , num_l))) # for simple operation can use lambda, complex operations can still drink directly def own definition of
# # because the map function directly output is an iterator, can not be directly printed, the list needs to operate with a method to directly print
# print (map (pingfang, num_l )) # output is: <Map Object AT 0x000001D030958518>
# # remember list operation performed


#filter function incorporated
# proposed requirements, there will be a list of sb in front of or behind another sb remove
# the old fashioned way:
PEOPLE_LIST = # [ 'Xiaoming', 'Xiaoli', 'xiaohong_sb', 'xiaoqiang_sb']
# # RES = []
# for # P in PEOPLE_LIST:
# # Not p.endswith IF ( 'SB'):
# # RES. the append (P)
# # Print (RES)
#
# # function references, just like map_test
# DEF end_test (X): # determines whether the ending SB
# Not x.endswith return ( 'SB')
# # DEF start_test (x): # determines whether the start SB
# Not x.startswith return ( 'SB')
# DEF filter_test (FUNC, Array):
# RES = []
# for P in Array:
# iF FUNC (P):
# RES .append (the p-)
# return RES
#
# Print (filter_test (end_test, PEOPLE_LIST))
# # this time can actually start using lambda, an anonymous function
Print # (filter_test (the lambda X: Not x.endswith ( 'SB'), PEOPLE_LIST))

# # ultimate version introduced last, Python function comes filter function
# # filter function written specification, filter (function method (return bool type value, returns True if, in the rear retention element parameters), parameters can be iterative)
## directly to get all of the above a
# print (list (filter (lambda x: not x.endswith ( 'sb'), people_list)) ) list using the same reason #
# print (list (filter (end_test , people_list))) # decided lambda, or from the def operator function defined according to the complexity of the operation
## with the same result

from the reduce functools Import

#reduce function introducing
# functools needs to be introduced from the inside to reduce
# requirements: num_l = [1,2,3,67,8,101] or a product of all the numbers and
# num_l = [1,2,3,100]
# num_l.pop RES = (0)
in num_l for NUM #:
# = RES + RES NUM
# Print (RES)

# the above method is too fixed manner reintroduced def

# num_l = [1,2,3,100]
# def Cheng (X, Y):
Return X * Y #
# DEF qiuhe (X, Y):
# return X + Y
# # DEF reduce_test (FUNC, Array, the init = None): #reduce initial value defaults to zero,
# # # Here is to be understood that and coupled to a product or by a number or numbers
# # Array.pop RES = (0)
# # I in Array for:
# # RES = FUNC (RES, I)
# return RES #
# #
# # Print (reduce_test (Cheng, num_l))
# # Print (reduce_test (the X-the lambda, the y-: the y-the X-+, num_l))
#
# # # at this time to introduce the ultimate method reduce (here and write their own method of run time error, have go into)
# num_l = [1,2,3,100]
# Print (the reduce (the lambda X, Y: X * Y, num_l))
# Print (the reduce (qiuhe, num_l))

the following are mentioned today Python built-in function
abs takes the absolute value of
all elements in each iteration may bool operator object, or if all True iterables empty, return True
any of iterables each element bool operator, if there is a return value is True, True is returned
bin turn the decimal binary
bool Boolean operations
bytes string into bytes in byte format (parameter, encoding = 'utf-8 / gbk etc. ')
decode the previous bytes contrary, the same writing format
chr corresponding ASCII code number
dict dictionary
dir display method corresponding to a directory
divmod writing format divmod (dividend, divisor) to give (quotient, a remainder), used to make tab
evol read data string structure, very strong (return the type of string, comprising a mathematical operation, also results)
a float floating-point number returns
hash may be a hash of immutable data, obtains the corresponding ha Xi value (hash value characteristics: a unique fixed length does not increase significantly with increasing length of the thrust reversers is not in accordance with the hash value data, the same contents of the same hash value) for detecting whether the file has been modified
according to the present method help explain display all
hex decimal turn hex
oct octal, decimal turn
isinstance detect whether the data type in the correct format: isinstance (number Type), the corresponding returns True
about locals display all local variables
globals displays all global variables

that these, these built-in method estimates still have to practice a little bit, just on the eval overturned, to sleep, bye


Guess you like

Origin www.cnblogs.com/xiaoyaotx/p/12393287.html