day12 function advanced operation

Posted by abhinic on Tue, 01 Mar 2022 13:08:30 +0100

summary

Anonymous function

  1. essence

    The essence of anonymous functions is still functions. They are the same except for the syntax of their definitions
    Note: it can only be used to implement functions with very simple functions

  2. Syntax:
    lambda parameter list: return value

    Convert to normal:
    def (formal parameter list):
    Return return value

Scope of variable

  1. global variable

    Variables not defined in functions and classes are global variables;
    The scope of global variable is from the beginning of definition to the end of program;
    Global variables are saved in the global stack interval, and will not be released automatically until the global stack interval is completed

  2. Local variables the variables defined in the function are local variables, including formal parameters
    The scope of a local variable is from the beginning of the definition to the end of the function
    When calling a function, the system will automatically create a temporary stack interval for the function to save the data generated in the function (including local variables),
    When the function call ends, it will be released automatically

  3. global keyword

    Function 1: you want to modify the value of global variables in the function

    Function 2: define a global variable in the function

A function is a variable

  1. Defining a function in python is actually defining a variable whose type is function, and the function name is the variable name
  1. Higher order function: real parameter higher order function, return value higher order function

Real parameter higher order function

  1. Max (sequence) – find the largest element in the sequence

    1). Max (sequence, key = function) – get the largest element in the specified according to the rules specified by the function
    Function requirements: 1) there is only one parameter (this parameter represents the element in the sequence)
    2) There is a return value (comparison object)

    2) Min (sequence, key = function) – obtain the smallest element in the specified according to the rules specified by the function
    3) Sorted (sequence, key = function) – sorts the elements in the sequence from small to large according to the rules specified by the function
    4)lieb. Sort (sequence, key = function) – sort the elements in the sequence from small to large according to the rules specified by the function

  2. Map (function, sequence) – transform the elements in the sequence according to the rules specified by the function to get a new sequence

    Function requirements: 1) there is only one parameter (representing the elements in the sequence)
    2) There is a return value (elements in the new sequence - in this case, it is only necessary to describe the relationship between the elements in the new sequence and the elements in the original sequence)

    Map (function, sequence 1, sequence 2)
    Requirements of the function: 1) there are only 2 parameters (representing the elements in the sequence respectively)
    2) There is a return value (elements in the new sequence - in this case, it is only necessary to describe the relationship between the elements in the new sequence and the elements in the original sequence)

    Map (function, sequence 1, sequence 2, sequence 3,...)

  3. RedOnce – sequence merge

    Reduce (function, sequence, initial value)
    Initial value: generally 0, 1 or empty string
    Function requirements: 1 There are and only two parameters (the first parameter points to the initial value; the second parameter points to each element in the sequence)
    2. There is a return value (determining the consolidation method)

    Note: you must use the pilot package before using reduce
    x is the initial value and item is the element

task

  1. Write an anonymous function to judge whether the specified year is a leap year

    result = lambda year:(year%4==0 and year%100!=0) or year%400==0
    print(result(2000))
    
  2. Write a function to reverse the order of elements in a specified list (such as [1, 2, 3] - [3, 2, 1]) (Note: do not use the reverse order function of the list)

    result = lambda list1:list1[::-1]
    print(result([1,2,3]))
    
  3. Write a function to calculate the sum of squares of the bits of an integer

for example: sum1(12) -> 5(1 Square of plus square of 2)    sum1(123) -> 14
nums1 = '123'
result = reduce(lambda x, item:(item)**2+x,nums1,0)
print(result)
  1. Find the element with the smallest absolute value in the list nums
For example: nums = [-23, 100, 89, -56, -234, 123], The maximum value is:-23
nums = [-23, 100, 89, -56, -234, 123]
result = min(nums,key=lambda item:-item if item < 0 else item)
print(result)
  1. We have two lists A and B. create A dictionary with the map function. The element in A is key and the element in B is value

    A = ['name', 'age', 'sex']
    B = ['Zhang San', 18, 'female']
    New dictionary: {'name': 'Zhang San', 'age': 18, 'sex': 'female'}
    
  2. The three lists represent the names, subjects and class numbers of five students respectively. Use map to spell the three lists into a dictionary representing the class information of each student

    names = ['Xiao Ming', 'floret', 'Xiao Hong', 'Lao Wang']
    nums = ['1906', '1807', '2001', '2004']
    subjects = ['python', 'h5', 'java', 'python']
    result:{'Xiao Ming': 'python1906', 'floret': 'h51807', 'Xiao Hong': 'java2001', 'Lao Wang': 'python2004'}
    
    result = dict(map(lambda i1,i2,i3:(i1,i3+i2),names,nums,subjects))
    print(result)
    
  3. A list message has been created. Use reduce to calculate the sum of all numbers in the list

    message = ['Hello', 20, '30', 5, 6.89, 'hello']
    Result: 31.89
    result = reduce(lambda x,item:x+item if type(item) in (int,float) else x,message,0)
    print(result)
    
  4. What has been saved in the points list is the coordinates of each point (the coordinates are represented by tuples, the first value is the x coordinate, and the second value is the y coordinate)

    points = [
      (10, 20), (0, 100), (20, 30), (-10, 20), (30, -100)
    ]
    

    1) Gets the point with the largest y coordinate in the list

    result = max(points,key=lambda item:item[-1])
    print(result)
    

    2) Gets the point with the smallest x coordinate in the list

    result = min(points,key=lambda item:item[0])
    print(result)
    

    3) Gets the point furthest from the origin in the list

    result = max(points,key=lambda item:item[0]**2+item[1]**2)
    print(result)
    

    4) Sort the points according to the distance from the point to the x-axis from large to small

    result = sorted(points,key=lambda item:-item[0] if item[0] < 0 else item[0],reverse=True)
    print(result)
    

Topics: Python