python3-notes-c-004-functions-map, filter, reduce & lambda

Source: Internet
Author: User

f =Lambda xY, z:x + y + Z
Print (f (1,2,3))# 6
g =Lambda x, y=2, z=3:x + y + Z
Print (g (1,z=4,y=5))# 10

# lambdaExpressions are commonly used to write jump tables (Jump table), which is a list or dictionary of behaviors
L = [(Lambda x:x**2),
(Lambda x:x**3),
(Lambda x:x**4)]
Print (l[0] (2), l[1] (2), l[2] (2))# 4 8 16

D = {' F1 ':(Lambda:2 +3),
' F2 ':(Lambda:*3),
' F3 ':(Lambda:2**5);
Print (d[' F1 '] (), d[' F2 '] (), d[' F3 '] ())# 5 6 8

# map (function, sequence[, sequence, ...]) list
#Maps a function call to the corresponding element of each sequence and returns a list with all the returned values
DefInc (X, y):
Return (x, y)
La = [1,2,3,4]
LB = [' Sun ',M,T,W,T,F,' S ']
L1 =ListMap (inc, LA, LB))# <class ' list ': [(1, ' Sun '), (2, ' M '), (3, ' T '), (4, ' W ')]
L2 =ListMap (Lambda x, y:x * y), LA, LA))#of two parameters<class ' list ';: [1, 4, 9, 16]
L3 =ListMap (Lambda x:x**2), LA))#UseLambdaForm<class ' list ';: [1, 4, 9, 16]
L4 =List (x**2For XInchRange1,5))#It's faster.<class ' list ';: [1, 4, 9, 16]
L5 =List (x+yFor XInchRange5)If X2 = =0For YInchRange10)If y%2 = =1)
# <class ' list ';: [1, 3, 5, 7, 9, 3, 5, 7, 9, 11, 5, 7, 9, 11, 13]

# Filter (function or None, sequence), list, tuple, or string
#A given Boolean function is called for each element of a known sequence, and an element that returns a nonzero value is added to a list
DefFUN1 (x):
If x >100:
Return True
Else
Return False
F1 =ListFilter (FUN1, [2, 101, 3]) # <class ' list ';: [101, 600]
F2 = List (filter (lambda x:x% 2 = = 0, LA)) # <class ' list ';: [2, 4]
F3 = List (filter (lambda x: True if (x > ' a ') else False, [' A ', ' B ', ' C ')]) # <class ' L Ist ';: [' B ', ' C ']

# Reduce (function, sequence[, initial]), value
# (Summary: The continuation of the result and the next element of the sequence are cumulative calculations)
# functionparameter is a function with two parameters,ReduceSequentially fromSequenceTo take an element from a
#And the Last CallfunctionThe result of making the parameter call againfunction。 First time callfunctionWhen
#If you provideInitialparameter, theSequenceThe first element in theinitial call function< Span style= "COLOR: #808080",
# otherwise sequence The first two elements in the parameter call function from functools import reduce
C = reduce (lambda x#
C = reduce (lambda x100) #

Python3-notes-c-004-functions-map, filter, reduce & lambda

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.