Python Basics 9 - Commonly Used Functions

1、Lambda

  ①The Lambda function body can only have one return value expression statement, the syntax is (variable number of parameters):

    return function name = lambda x, y, z: x+y+z

  ②It can be used as an element of an array (or a dictionary or a list), oh that person implements the table transfer function, that is, a function list

    Definition syntax: array name = [(lambda expression 1), (lambda expression 2)...]

    calling method: arrayname[index] (parameter list of lambda expression)

1 he = lambda x, y, z: x + y + z
 2  print (he (1,2,6 ))
 3 arr1 = [( lambda x: x ** 2), ( lambda x: x ** 3), ( lambda x: x ** 4 )]
 4  print (arr1 [0] (2), arr1 [2] (2))

  ③ As the return value of the function

 1 def jisuan(x):
 2     if x==1:
 3         return lambda x,y:x+y
 4     if x==2:
 5         return lambda x,y:x-y
 6     if x==3:
 7         return lambda x,y:x*y
 8     if x==4:
 9         return lambda x,y:x/y
10 
11 action=jisuan(1)
12 print(action(10,2))
13 action=jisuan(2)
14 print(action(10,2))
15 action=jisuan(3)
16 print(action(10,2))
17 action=jisuan(4)
18 print(action(10,2))

2、map()

  The map function is used to call the specified function with all the elements in the specified sequence as parameters, and form the result into a new sequence. The return syntax is as follows:

    result sequence = map(map function, sequence1[, sequence2, ...])

1 arr1=map(lambda x:x**2,[2,4,6,8,10])
2 for e in enumerate(arr1):
3     print(e)
4 
5 arr2=map(lambda x,y:x+y,[1,3,5,7,9],[2,4,6,8,10])
6 for e in enumerate(arr2):
7     print(e)

3、filter()

  The filter function can perform filtering operations on the specified sequence, and the specific definitions are as follows:

    filter = (function function, sequence sequence)

    The function functionjj accepts a parameter and returns a bool, the sequence can be a list, a tuple, or a string

    The filter function is a sequence of parameters. Each element in the sequence is a parameter to call the function function, and the element whose call result is True will be returned as the result of the filter function.

1 def is_even(x):
2     return x%2==0
3 arr=filter(is_even,[1,2,3,4,5,6,7,8,9,10])
4 
5 for e in enumerate(arr):
6     print(e)

4、reduce()

  Call the specified function with all elements in the specified sequence as parameters according to certain rules, the syntax is as follows:

    compute result = reduce(mapping function, sequence)

    There must be two arguments to the map function, reduce is called first with the first and second elements of the sequence, then with the result of the first and second elements and the third element, and so on

1 from functools import reduce
2 def myadd(x,y):
3     return x+y
4 sum=reduce(myadd,(2,4,6,8,10))
5 print(sum)

5、zip()

  Take some lists of columns as parameters, pack the corresponding elements in the list into tuples, and then return a list composed of these tuples (if the length of the incoming parameters is different, the length of the returned list is the same as the shortest parameter)

1 a=[ 1 , 2 , 3 ]
 2 b=[ 4 , 5 , 6 ]
 3 zipped= zip(a,b)
 4  for element in zipped:
 5      print(element)
 6 print( ' ------ -------- ' )#The result before and after the dotted line is the same
 7 b=[ 4 , 5 , 6 , 7 , 8 , 9 ]
 8 zipped= zip(a,b)
 9  for elementin zipped:
10     print(element)
1 a=[1,2,3]
2 b=[4,5,6]
3 zipped=zip(a,b)
4 unzipped=zip(*zipped)
5 for element in unzipped:
6     print(element)

 

Guess you like

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