Higher-order features

Higher-order features of python, including slices, list comprehensions, generators, and iterators.   

slice

Slicing is mainly a feature of sequences, and sequences (lists, tuples, strings) can be accessed using slices.

 
list comprehension

List comprehensions are very simple and powerful built-in comprehensions in python that can be used to create lists.
        For example: [x * x for x in range(1,11)] #Square
            [x * x for x in range(1,11) if x % 2 == 0] #Filter out the square of even numbers
            [m + n for m in 'ABC' for n in 'XYZ'] #All arrangement
            import os
            [d for d in os.listdir('.')] #List all files and directories in the current directory
            
            d = {'x': 'A', 'y':'B', 'z':'C'}
            [k + '=' + v for k,v in d.items()]
            
            L = ['Hello', 'World', 'IBM', 'Apple']
            [s.lower() for s in L]
     

       
Builder

Create a generator,
        method 1: Change the [] of a list comprehension to (), and a generator is created.
                For example: g = (x * x for x in range(10))
                The generator saves the algorithm. Each time next(g) is called, the value of the next element of g is calculated until the last element is calculated.
                However, normally, instead of using the next() function, a for loop is used because generators are also iterables.
        Method 2: Use yield. If a function definition contains the yield keyword, the function is no longer an ordinary function, but a generator.    
                For example: Fibonacci function
                    def fib(max):
                        n,a,b = 0,0,1
                        while n < max:
                            print(b)
                            a, b = b, a+b
                            n = n + 1
                        return
                changes to a generator:
                    def fib(max):
                        n, a, b = 0, 0, 1
                        while n < max:
                            yield b
                            a, b = b, a+b
                            n = n + 1
                        return
        Difficulty: the execution flow of generator and function is different of. Functions are executed sequentially, and the return statement or the last line of function statement is encountered.
            After the function becomes a generator, it is executed every time next() is called, and it returns when it encounters a yield statement. When it is executed again, it continues to execute from the yield statement returned last time.
            
        Similarly, after changing the function to generator, basically never use next() to get the next return value, but directly use the for loop to iterate.
        But after using the for loop to call the generator, it is found that the return value of the return statement of the generator cannot be obtained. If you want to get the return value, you must catch the StopIteration error, and the return value is contained in the value of StopIteration.
        
        Summary: A generator is a very powerful tool. You can simply change the list comprehension into a generator, or you can use a function to implement a generator with complex logic.
            To understand the principle of the generator, it continuously calculates the next element in the process of the for loop, and ends the for loop with the appropriate condition. For the generator that the function is changed to, encountering the return statement or executing the last line of the function body is the instruction to end the generator, and the for loop ends accordingly.
            Note the distinction between ordinary functions and generator functions. An ordinary function call directly returns the result, and a generator function call actually returns a generator object.     

          
iterator


        The data types that can be directly applied to the for loop are as follows:
            1. Collection data types, such as list, tuple, dict, set, str, etc.
            2. Generator, including generator and generator function with yield.
                These objects that can act directly on the for loop are collectively called iterable objects: Iterable.
            You can use isinstance() to determine whether an object is an Iterable object. The generator can not only act on the for loop, but also be continuously called by the next() function and return the next value, until the StopIteration error is finally thrown, indicating that the next value cannot be returned.
            
            An object that can be called by the next() function and continuously returns the next value is called an iterator: Iterator.
            You can use isinstance() to determine whether an object is an Iterator object.
            
            from collections import Iterable
            from collections import Iterator
            
            Generators are Iterator objects, but although list, dict, and str are Iterables, they are not Iterators.
            To turn iterables such as list, dict, str, etc. into Iterators, you can use the iter() function.
            
            The Iterator object is a data stream. The Iterator object can be called by the next() function and continuously returns the next data until there is no data, and a StopIteration error can be thrown.
            
            Iterator can even represent an infinite stream of data, such as all natural numbers. And using list is never possible to store all natural numbers.
   

Guess you like

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