Python Learning note-day04-Part III (built-in function, Map,filter,reduce,yield)

Source: Internet
Author: User

Map
Iterate through the sequence, manipulate each of these elements, and finally get a new sequence
LST =[1,2,3,4]
def fun (ARG):
Return arg+10
New_lst = Map (fun,lst)


def fun2 (A1,A2):
Return A1+A2
lst01 = [11,22,33]
lst02 = [A]
Map (func2,lst01,lst02) #序列的长度需要一致

or map (lambda a1,a2,a3:a1+a2,lst01,lst02)


#################################################################
Filter
There are two parameters, the first parameter is a function or none the second argument is a sequence
Receives a function or none, returns a collection of elements that match the criteria


LST = [1,2,3,4,5,0]
Print filter (None,li) # Iterates through all the elements and filters out the elements that are false for the Boolean value, at which point the boolean is the true element.
Output of elements greater than 3
Print filter (lambda a:a>3,lst)

#################################################################


Reduce
Accumulates the elements within the sequence, returning a result

# The first parameter of reduce, the function must have two parameters
# The second parameter of reduce, the sequence to loop
# The third parameter of reduce, the initial value


Li = [11, 22, 33]

result = reduce (lambda arg1, Arg2:arg1 + arg2, Li)

# The first parameter of reduce, the function must have two parameters
# The second parameter of reduce, the sequence to loop
# The third parameter of reduce, the initial value



#################################################################


Yield generator
Remember the last action, the next time you execute, you can continue to execute

def fun ():
Yield 1
Yield 2
Yield 3
For I in Fun ():
Print I

Results:
1
2
3

Custom generators:

Def myrange (num):
Seed =0
While True:
If Seed>=num:
Break
Seed+=1
Yield seed

For I in myrange (10):
Print I

This article is from the "'ll Notes" blog, so be sure to keep this source http://timesnotes.blog.51cto.com/1079212/1715266

Python Learning note-day04-Part III (built-in function, Map,filter,reduce,yield)

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.