The many uses python function

1. the lambda anonymous function 
Syntax:
Variable = lambda Parameters: Return Value
can perform complex operations
2. sorted () function
sort:
1. iterables
2. key = function, collation
3. reverse, if reverse
3. filter ( ) function
filters
1. function that returns True or False
2. iterables
4. map () mapping function
1. function
2 iterables



1. the lambda anonymous function
# The lambda: behind the body of the function (corresponding to the content later return) 
# A parameter 
C = the lambda A: A * A        # line to get a function, however, can not function to perform complex operations, C corresponding to the function name 
Print (C)         # corresponds to the function name to access lambda 
Print (C (. 5 ))
 Print (. C the __name__ )    # View function name, c is a variable 

C = lambda a, B: a + B
 Print (C (1,2))


2. sorted function
= LST [3,34,1,14,5,23,15,25,30,12,41 ] 
lst.sort () 
Print (LST)   # result is a sequence 
LL = the sorted (LST, Reverse = True) # built function, you returned to a new list, the new list is sorted 
Print (LL)    # result in reverse order, reverse = True descending 

# to sort the list, sorted according to length of the string 
# sorted according to a number of 
lst = [ " American past aaa " , " wolf aaaa " , " wandering the Earth aaaaaa " , " Western zombie aaaaaaaa " , " airplane battle tanks aaa " ]
 DEF FUNC (S):
     #return len (s) # sorted according to the length of the 
    return   s.count ( " A " )          # Returns 
RET = the sorted (LST, Key = FUNC) # inside objects iteration pass in each element to func, key represents collation, what sort function 
#              iterables function 
Print (RET) 

# sort (anonymous function and sorted () function) according to the number of a 
LL the sorted = (LST, Key = the lambda S: s.count ( " a " ))     # amount calculating a also be sorted according to a digital 
Print (LL) 

# the length ordering (with anonymous function and sorted () function) 
AA = the sorted (LST, Key = the lambda a: len (a))    # is calculated the length is also ordered numbers of 
print(AA) 

# variable = sorted (iterable, key = function name, reverse = True (reverse))
 

 

3. filter function

= LST [1,2,3,4,5,6,7,8,9]    # Analyzing odd 
DEF FUNC (A):
     return A. 1% 2 ==   # must be True, False, True is leaving the element 

AA = filter (FUNC, LST)       # AA second parameter is the iterator must iterables 
# first parameter function, the second parameter to the function of each element, the function returns True if, leaving this element 
Print ( " the __iter__ "  in the dir (AA))     # determines whether the iteration 
Print ( " __next__ "  in the dir (AA))     # determines whether the iterator 
Print (List (AA)) 

A = filter ( the lambda A: . 1% 2 == A , LST)
for I in A: # A is an iterator 
    Print (I)

 

4. map mapping function

= LST [1,2,3,4,5,6,7,8,9 ]
 DEF FUNC (I):
     return   I * I
 # Map () is a mapping function for processing data into 
a = map (func, lst)        # a iterators 
Print (List (a)) 

Print (List (Map ( the lambda a: a * a, LST))) # the iteration objects in each element is transmitted for processing to the foregoing functions, 
#    treated results are returned 

LST1 = [1,2,3,4,5 ] 
lst2 = [2,4,6,8 ,] 

IT = Map ( the lambda X, Y: X + Y, LST1, lst2)
 Print (List (IT) )      # If there are multiple function parameters, corresponding to the back of the list of correspondence, like zip, is summed with the minimum.

 

5. filter map and the difference between:

LST = [ 
    { " Movie " : " Iron Man " , " name " : " Bob " , " Age " : 18 is }, 
    { " Movie " : " Avengers " , " name " : " small yellow " , " Age " : 38 }, 
    { " movie " : " American past . ",'name' : " Small day " , " Age " : 48 }, 
    { " movie " : " Shanghai fortress " , " name " : " Wang " , " Age " : 58 }, 
    { " movie " : ' Wolf 1 ' , " name " : " Sally " , " Age " :, With 28 } 
    ] 

'aa= Map ( the lambda A: A [ " Age " ] <40 , LST)
 Print (List (AA)) 
X = filter ( the lambda DIC: DIC [ " Age " ] <40 , LST)
 Print (List (X)) 

# difference 
# filter, map 
# when the map data can be compared returns True and False 
# filter data comparison is True leaves the element,

 

# Completed




Guess you like

Origin www.cnblogs.com/Pengdachui-1/p/11587262.html