Functions (3)

Source: Internet
Author: User
Tags mul

Recursive

means of using the function itself in the definition of a function

Non-Fibonacci sequence recursion function:

def fib (n)ifn = =0:        return 0elif N==1:        return 1    Else:        returnFIB (n2) +FIB (n1)if__name__ = ="__main__": F= FIB (Ten) Print F

As you can see from the above process, each recursive process is a step closer to the initial known condition, a0=0,a1=1 until the results are obtained through this bottom-level condition, and then the results are returned to the computer in a layer.

Because a0=0,a1=1 it is known, it is not necessary to judge one side at a time. So, you can also optimize. The basic scenario for optimization is to initialize the initial two values.

Meno = {0:0,1:1}def fib (n):    if in meno:        = FIB (n1) + FIB (n2)    return  meno[n]if " __main__ " :    print fib (ten)

Several special functions

Python is a language that supports a variety of paradigms and can perform so-called functional programming, which is highlighted by several functions:

filter, map, reduce, lambda, yield

L

Lambda

A lambda function, a function that solves a problem in just one line

Lambda Arg1, arg2,.. . argn:expression using Arguments

    • followed by a variable directly behind a lambda
    • The variable is followed by a colon
    • The colon is followed by an expression, and the result of the expression evaluates to the return value of the function.

Although a lambda function can receive any number of arguments (including optional arguments) and return the value of a single expression, the lambda function cannot contain commands and cannot contain more than one expression. Do not attempt to cram too much into the lambda function; If you need something more complicated, you should define a normal function, and then you want to make it long enough.

>>> lamb = [Lambda x:x,lambda x:x**2, Lambda x:x**3, Lambda x:x**4  ]for   in lamb:     ... Print I (339Bayi

Map

Map (FUNC,SEQ), Func is a function, and SEQ is a sequence object. At the time of execution, each element of the sequence object is taken out in order from left to right, and then loaded into the Func function, and the return value of func is stored in a list in turn.

>>> items = [1,2,3,4,5]>>> squared = []>>> forIinchitems: ... squared.append (i**2)... >>>squared[1, 4, 9, 16, 25]>>>defSQR (x):returnX**2... >>>map (sqr,items) [1, 4, 9, 16, 25]>>> Map (LambdaX:x**2, items) [1, 4, 9, 16, 25]>>> [x**2 forXinchItems]#This is my favorite, in general the speed is fast enough, and the readability of the strong[1, 4, 9, 16, 25]
>>> lst1 = [1,2,3,4,5]>>> lst2 = [6,7,8,9, 0]>>> map (Lambda x,y:x +y, Lst1,lst2)     # Add the corresponding items from the two list and return a list of results [7, 9, 11, 13, 5]
>>> lst1 = [1,2,3,4,5]>>> lst2 = [6,7,8,9, 0]>>> lst3 = [7,8,9,2,1] >>> Map (Lambda x,y,z:x+y+z, lst1,lst2,lst3) [14, 17, 20, 15, 6]
Reduce
>>> reduce (lambda x,y:x+y,[1,2,3,4,5])15
>>>a[3, 9, 8, 5, 2]>>>b[1, 4, 9, 2, 6]>>> Zip (A, B)#To review the zip, use the following method[(3, 1), (9, 4), (8, 9), (5, 2), (2, 6)]>>> sum (x*y forX, yinchZip (A, b))#Direct summation after parsing133>>> new_list = [x*y forX, yinchZip (A, b)]#can be seen as a distribution implementation of the above method>>>#This parsing can also be: New_tuple = (x*y for x, y in Zip (A, b))>>>new_list[3, 36, 72, 10, 12]>>> sum (new_list)#or: Sum (new_tuple)133>>> Reduce (LambdaSum, (x, y): Sum+x*y,zip (A, B), 0)#is this a cool way to play? 133>>> fromoperatorImportAdd,mul#There's more than one way to play cool.>>>Reduce (Add,map (mul,a,b))133>>> Reduce (LambdaX,y:x+y, Map (LambdaX,y:x*y, B))#Map,reduce,lambda are all complete, is it even cooler? 133

Special reminder: If the reader is using Python3, it is a bit different from the above, because in Python3, reduce () has been removed from the global namespace and placed in the Functools module, if used, it needs to be from functools import reduce introduced.

Filter
>>> numbers = range ( -5,5)>>>numbers[-5,-4,-3,-2,-1, 0, 1, 2, 3, 4]>>> Filter (LambdaX:x>0, numbers) [1, 2, 3, 4]>>> [x forXinchNumbersifX>0]#equivalent to the above sentence[1, 2, 3, 4]>>> Filter (Lambdac:c!='I','Qiwsir')#can you tell me the same thing about the above document? 'QWSR'           

Functions (3)

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.