Built-in functions map ()

Function: map ()  will do the mapping function in accordance with the specified sequence provided. map () is a function of two parameters of a name, a list or other tuples. Results of treatment, is not a position change list element

 map the second transfer function parameters as long as the line is iterables

grammar

map () function syntax:

map(function, iterable, ...)

parameter

  • function - Function
  • iterable - one or more sequences

 

1. The requirements to obtain a new list, the square value of the original element value of the element ------> implemented for loop

NUM_1 = [1,2,10,5,4,6 ]
 print (NUM_1) 
law = []
 for the in NUM_1: 
    ret.append (i ** 2 )
 the printer (right)

2. The light version list elements squared function implemented functions Package

NUM_1 = [1,2,10,5,4,6 ]
 print (NUM_1)
 def map_test (arry): 
    right = []
     for the in NUM_1: 
        ret.append (i ** 2 )
     return   the right 
right = map_test (NUM_1 )
 print (right)

3. The final version implemented using an anonymous function by value 1 plus function elements and achieve encapsulation

num_1 = [1,2,10,5,4,6]
def map_test(func,arry):
    ret = []
    for i in arry:
        res = func(i)
        ret.append(res)
    return ret
print(map_test(lambda x:x+1,num_1))

4.map function: can be transferred lambda x: x + 1 in the form of a function, custom function may be passed the name of the actual

Example: Using the map function implementation elements in the sequence +1

= NUM_1 [1,2,10,5,4,6 ] 
RES = Map ( the lambda X: X +. 1 , NUM_1)
 Print ( ' built-in function map, the processing result ' , RES)
 Print (List (RES))

Behind 5.map function with self-defined functions

= NUM_1 [1,2,10,5,4,6 ]
 DEF reduce_one (X):
     return X. 1-
 Print ( ' transfer function is known ' , List (Map (reduce_one, NUM_1)))

6.map () is a function of two parameters of a name, and a list of tuples or string may be.

 

= MSG ' lihaifeng ' # string 
Print (List (Map ( the lambda X: x.upper (), MSG)))

 

Guess you like

Origin www.cnblogs.com/renzhiqiang/p/10930539.html