Python six days, lambda expression learning, involving filter and map.

Source: Internet
Author: User
Tags closure

In Python, lambda expressions can be used as anonymous functions, with a simple chestnut:

We used to write two numbers to add a function that needed

# we used to write two numbers to add the function, need to write this def sum (x, y    ): return x+y>>> sum# and after we've learned about lambda, we'll write this function again . >> > x=lambda x,y:x+y>>> x

Python's implications for our future programming, when using Python to write scripts, you can save the process of defining functions using anonymous functions, such as when we write a simple script to manage server time, we don't need to define a function specifically, and then write a call, Using anonymous functions can make your code more streamlined.

For some of the more abstract and the entire program to execute down only one or two calls, sometimes to the function name is also a headache, using anonymous functions do not need to consider the problem.

Simplifying the readability of the code, ordinary functions often jump to the DEF definition section at the beginning, and using anonymous functions saves this step.

Practice a few more games

" " def fun_a (x,y=3)    return X*y Use this method to write "Lambda x,y=3:x*y with a lambda expression" Print (Fun_a (2)) Print (Fun_a (2,6))
" " Lambda x:x If x%2 else none becomes a function " " def Odd (x):     if x%2:        return  x    Else:        return  Noneprint(odd (9))

Filter, filters, can help us filter out something we don't want.

" " use filter and lambda expressions to quickly find multiples of 3 within 100 " "  = filter (lambda x: Not (x%3), Range (0,100)) Print (list (fun_b))     
" " use Zip to bind two arrays to a list (Zip ([1,2,3,4,5],[6,7,8,9,10])) [(1,6), (2,7), (3,8) , (4,9), (5,10)] " " Print (List (map (Lambda x,y:[x,y], (1,2,3,4,5), (6,7,8,9,10))))
def make_repeat (n):     return Lambda x:x*n# closure function, the following call is equivalent to assigning n a value of 2double = Make_repeat (2)# The closure is called and the X in the closure is assigned a value of 8 Print (Double (8)) Print (Double ("))

The result is:

612------------------------------9------------------------------3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, A,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, and------------------------------  [[1, 6], [2, 7], [3, 8], [4, 9], [5, ten]]----------------16

Python six days, lambda expression learning, involving filter and map.

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.