Beginner Python (12)--higher Order function

Source: Internet
Author: User

Beginner Python (12)--higher Order function

Beginner Python, mainly finishing some of the learning points, This is the Higher-order Function.

#-*-coding:utf-8-*-    " "higher order functions: functions that can be used as parameters are called Higher-order functions." "  #function as parameterdefF (x):returnx*x#the map function is a built-in function, meaning that the list of the second argument is in the F function#The final result is a listPrintMap (f,[1,2,3,4,5])  #the reduce function is a built-in function, meaning that the sequence of the second parameter acts to the value of the Add function#the result is calculated as a summation, and the final result is a numberdefAdd (x, y):returnx+yPrintReduce (add,[1,2,3,4,5,6])  #give a number on each position of an integer to get the integer  deffn (x, y):returnx*10+yPrintReduce (fn,[1,2,3,4,5])    #string goto int  PrintReduce (fn,map (int,'12345'))    defStr2Int (s):deffn (x, y):returnx*10+yreturnreduce (fn,map (INT,S))PrintStr2Int ('123456')  " "the procedure for calling the above function Is: 1. Get reduce (fn,map (int, ' 123456 ')) 2. Get reduce (fn,[1,2,3,4,5,6]) 3. Get reduce (x*10+y,[1,2,3,4,5,6]) 4. Get 123456" "  defStr2int2 (s):returnReduceLambdax,y:x*10+Y,map (INT,S))PrintStr2int2 ('1234567')      #SortPrintSorted'313568')  PrintSorted ((1,8,4,2,5))  PrintSorted ([9,8,7,6,5,4,3])  PrintSorted (['name',' age','Sex','Address'])  #Sorted is also a high-order function, so it can also transfer functions to change the sorting algorithm#ReversedefInverted_order (x, y):ifX>Y:return-1elifx<Y:return1Else:          return0PrintSorted ((1,8,4,2,5), inverted_order)#changing the string sorting algorithm#the above string sorting is based on the ASCII code to determine the size#because uppercase letters are smaller than the ASCII code of lowercase letters,#but in general, we're all in alphabetical order.#Let's change the algorithm, ignoring the Case.  defAlphabet (s1,s2): L1=s1.lower () L2=S2.lower ()ifL1 <l2:return-1elifL2 <l1:return1Else:          return0PrintSorted (['name',' age','Sex','Address'],alphabet)" "the ' function as the return value actually involves the form of a function as a return value just before the string is transferred to int" "    defSum (*args): sum=0 forNinchArgs:sum+=NreturnsumPrintSum (1,2,3,4,5)    #converted to return function form  defSum_pack (*args):defsum (): sum1=0 forNinchArgs:sum1+=Nreturnsum1returnSum g= Sum_pack (1,2,3,4,5)  Printg#<function sum at 0x0134c0f0>#g output as function, want to print result to call functionPrintg ()" "definition of closure: word function automatically gets the variables and parameters of the parent function it's MINE. and each call returns a function that is not the same object" "G1= Sum_pack (1,2,3,4,5) G2= Sum_pack (1,2,3,4,5)  PrintG1==g2

Beginner Python (12)--higher Order function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.