The use of python built-in functions & anonymous functions

View python [built-in functions] :
https://docs.python.org/zh-cn/3.7/library/functions.html

1. Python built-in functions

Python provides a lot of built-in functions, and briefly introduces several commonly used built-in functions.
For example: filter function, map function, exec function, all and any functions, zip function

1. filter function: function to filter data

  • Basic format: filter (parameter 1: filter rule function, parameter 2: iterable object)

    Example 1: Get data greater than 30 in the list

    li = [11, 33, 34, 55, 7, 2, 7, 66, 899, 7, 5]</p> <ol> <li> <p>Implement using list comprehension: print([i for i in li if i > 30])</p> </li> <li> <p>Use the filter function to achieve res = filter(lambda x: x > 30, li) print(list(res))

  • Results of the:

2. map function

  • Basic format: map function: (parameter 1: processing function, parameter 2: iterable object)

    Example 2: Quadratic method to calculate all data in a list

    li2 = [ 11 , 33 , 34 , 55 , 7 , 2 , 7 , 66 , 899 , 7 , 5 ]</p> <ol> <li> <p>Implement using list comprehension: print([i**2 for i in li])</p> </li> <li> <p>Use the filter function to achieve res2 = map(lambda x: x ** 2, li2) print(list(res))

  • Results of the:

3. exec function: execute python code

  • Scenario:

    1. The python code in the txt text can be executed

    </p> <h1>Execute the code in the txt file</h1> <p>with open ( 'filename.txt' , 'r' , encoding = 'utf-8' ) as f: exec(f.read())

    1. Can execute python code inside the string

    code = ''' def work(a, b): print(a + b)</p> <p>a = 200 b = 300 work(a,b) ''' </p> <h1>exec executes the code in the string</h1> <p>exec(code)

4. all function: iterates all elements in the object to be true and returns True

  • Equivalent to and conditions, used to simplify and more conditions

Example 3:

def work(name=None, age=None, sex=None):

    # Regular syntax and implementation of 
    if name and age and sex:
        print( '--established 01---' )

    # Use the all function to implement 
    if all([name, age, sex]):
        print( '--established 02---' )

5. any function

  • Equivalent to the or condition, used to simplify the condition with more or

Example 3:

def work(name=None, age=None, sex=None):

    # Regular syntax or implementation 
    if name or age or sex:
        print( '--established 01---' )

    # Use the any function to implement 
    if any([name, age, sex]):
        print( '--established 02---' )

6. zip function: aggregate packaging

Example 4: There are two lists, and now the two lists need to be combined into one list, which is reflected in the form of (11, ‘a’), (22, ‘b’)

s1 = ( 11 , 22 , 33 , 44 , 55 )
s2 = ('a', 'b', 'c', 'd', 'e', 'f', 'g')

res = zip(s1, s2)
print(list(res5))

result:

2. Anonymous functions

  1. Anonymous functions: lambda functions

  2. Usage scenarios: functions with very simple function body structure (functions with simple structure are used when passed as parameters)

  3. Basic syntax:

– Expression 1: lambda parameter 1, parameter 2: return value

Eg:

1. Conventional syntax implements a simple addition and subtraction function
 def  work (a, b) : 
    return a * b

print(work(11,22))

2.l mbda function implementation:
work2 = lambda a, b: a + b
print(work2(11,22))

– Expression 2: key=lambda x: x[value]

For example: sort the list below (rule: sort according to the size of the second value of each element in the list)

li = [[1, 10, 32], [3, 22, 99], [2, 33, 77]]

1.l mbda function implementation:
li.sort()      # sort by first value 
li.sort(key= lambda x: x[ 1 ])          # sort by second value 
li.sort(key= lambda x: x[ 2 ])          # Sort by third value 
print(li)

Leave a Comment

Your email address will not be published. Required fields are marked *