map built-in functions, lambda expressions, quickly generate the desired list, filter built-in functions

  map function                            

grammar

  map(function, iterable,...)

parameter

  • function – the function, with two arguments
  • iterable -- one or more sequences

return value

  Python 2.x returns a list.

  Python 3.x returns iterators.

1  def square(x) :             #calculate square 
2      return x ** 2
 3  
4 map(square, [1,2,3,4,5]) #calculate    the square of each element of the list 5 6 #use lambda expression 7 map ( lambda x: x ** 2, [1, 2, 3, 4, 5])   # use lambda anonymous function 8 9 map( lambda x, y: x + y, [1, 3, 5, 7, 9] , [2, 4, 6, 8, 10])
 
 

 

  lambda                            

Use lambda expressions to implement ternary operator

1  #The effect to be achieved 
2  if 0 == 0:
 3      name = " timo " 
4  else :
 5      name = " nana " 
6  
7  # lambda implementation If the if condition is true, the name is equal to the value in front of the if, otherwise it is equal to the following 
8 name = " timo "  if 0 == 0 " nana "

Implement simple functions using lambda expressions

1  #The effect to be achieved 
2  def func(arg):
 3      return arg + 1
 4  
5  #Execute 6 func(123 )
 7 8 9 #Use lambda expression to implement 
lambda followed by the parameters of the function, followed by the colon The return value is 10 lambda_func = lambda arg: arg + 1
 11 12 #Execute 13 lambda_func (123) 
 
 
 
 

  Quickly generate the desired list                        

generate a list with stride 5

1 arr = [x*5 for x in range(5 )]
 2  #Get the result 
3 arr = [0, 5, 10, 15, 20]

Combine lambda expressions to generate lists

1  #First case 
2 f0, f1, f2 = [ lambda m: m*i for i in range(3 )]
 3  
4 f0(1) == f1(1) == f2(1) == 2
 5  
6  
7  #The second case 
8  
9 f = [ lambda m: m*i for i in range(3 )]
 10  
11  for l in f:
 12      l(1 ),    
 13  
14  #result 2 2 2 
15  
16  
17  # Third case 
18 
19 f = ( lambda m: m*i for i in range(3 ))
 20  
21  for l in f:
 22      l(1 ),    
 23  
24  #result 0 1 2 
25  
26  
27  #fourth case 
28  
29 f0, f1, f2 = ( lambda m: m*i for i in range(3 ))
 30  
31 f0(1) == f1(1) == f2(1) == 2

The reasons for the different results in the above four cases

The third case is not a tuple, but a generator is returned. When calling l(1), it is during the execution of the generator, so the i variable in the function l changes with the iteration of the generator, so The end result will vary.
In the fourth case, the generator is iterated at once. Similar to the first and second, all the codes are executed, and i is at the last value of 2, so the value does not change.

  filter built-in function                            

describe

  The filter()  function is used to filter the sequence, filter out the elements that do not meet the conditions, and return a new list of elements that meet the conditions.

  This receives two parameters, the first is a function, the second is a sequence, each element of the sequence is passed as a parameter to the function for judgment, then returns True or False, and finally puts the element that returns True into a new list.

grammar

    filter(function, iterable)

parameter

 

  • function – Judgment function.
  • iterable – an iterable object.

return value

  Back to list.

Example

 

1  def fun(s):
 2      if s != ' a ' :
 3          return s
 4 str = [ ' a ' , ' b ' , ' c ' ]
 5 res = filter(fun, str1) #filter             to character a 
6  
7  print (res) #returns             an iterable object

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324609124&siteId=291194637