Zero Foundation Python7 - Higher Order Grammar (Derivation, Iterator, Generator)

1. Derivation

1. Introduction of Derivation

There is a unique syntax in python that is the derivation (also known as the parse). Derivative formulas are constructed from one data sequence to another. There are three types of derivations: list derivation, dictionary derivation, and set derivation.

2. List Derivation

Basic syntax: [out_express for out_express in input_list]

For example: Generate a list of [0,1,4,9,16]. Use the list derivation code as follows:

odd_list = [i*i for i in range(5)]
print(odd_list)

We can see that list derivation is very convenient, and it may take a few lines of code to implement if you are using loops, but now you can do it in one line.

And when the list derivation still needs to satisfy certain conditions to output the expression, it can be implemented through the following syntax specifications:

[out_express for out_express in input_list if out_express_condition]

For example: li = [6,2,6,7,-15,8,-17,-10,-15,-4], squares the elements less than 0 in the list and saves them in a new list.

li = [6,2,6,7,-15,8,-17,-10,-15,-4]
new_li = [i**2 for i in li if i < 0]
print(new_li)

Nested Loop Derivation: [i for row in matrix for i in row]

Examples: The generated list li is ['1a','1b','1c','2a','2b','2c','3a','3b','3c'] with the following codes:

li = [ i + j for i in '123' for j in 'abc']

2. Dictionary Derivation

Basic syntax: {out_exp_key: out_exp_value for out_exp in input_list}

Example: the key value of a dictionary is an index of the elements of a list li, the value value value of a dictionary is an element of a list Li = ['age','name','gender']

The code is as follows:

li = ["age","name","gender"]
dic = {li.index(i):i for i in li}
print(dic)

Dictionary derivation can quickly interchange K and V in a dictionary. The code is as follows:

dic1 = {0:"0",1:"1",2:"2"}
dic2 = {v:k for k,v in dic1.items()}
print(dic2)

3. Set Derivation

Basic syntax: {out_exp_res for out_exp in input_set}

Example: Randomly generate 10 elements between 1 and 100 with weight removed

The code is as follows:

s1 = {random.randint(1,100) for i in range(10)}
print(s1)
print(len(s1))

Finally, it should be noted that python has no tuple derivation. Pthon's tuple derivation is called generator, which is another difference from the derivation.

Iterators

1.python iterator

Iterators are tools for iterating values. Iteration is a repetitive process. Each repetition is based on the previous result. Iteration provides a general index-independent iteration method of value.

2. Iterable Objects

(1) Objects that can be traversed with a for loop are iterative objects.

(2) str, list, tuple, dict, set, etc. are iterative objects.

(3) Generator, including generator and generator functions with yield ing.

3. Determine if iteration is possible

Except to see if u is built-in iter_u In addition to the method of determining whether the object is an iterative object, we can also use isinstance() to determine whether an object is an Iterable object. The function used in this case is similar to type() in determining whether or not it is the corresponding type.

from collections import Iterable,Iterator
print(isinstance('abc',Iterable))   # True
print(isinstance([1,2,3,4],Iterable))   # True
print(isinstance(123,Iterable))     # False   

4. Iterator Objects

(1) Has built-in_u next_u () The object of the method, which can be executed independently of the index value.

(2) Has built-in_u iter_u () Object of the method, execute iterator_u iter_u () The method still obtains the iterator itself.

It is important to note that objects are iterative, not necessarily iterators.

from collections import Iterable,Iterator
li = [1,2,3,4]
print(isinstance(li,Iterator)) # False

5.iter()

An object that can be called by the next() function and constantly returns the next value is called an iterator: Iterator. So we can turn Iterable objects into iterators using the iter () method.

Note: Iterators cannot subscript to values but use u Next_u () or next() function. But any time it goes beyond the scope of the iterator, a direct error will be reported. And next() can only defer the call, not move forward.

print(lis[0])    # Error not subscriptable
print(lis.__next__())
print(lis.__next__())
print(lis.__next__())
print(lis.__next__())

print(next(lis))
print(next(lis))
print(next(lis))
print(next(lis))  
print(next(lis))  #Error'list_ Iterator'object is not subscriptable

 

6. Differences between Iterable Objects and Iterators

(1) All iterative types are available for the for loop

(2) Acting on next() is an iterator type

(3) list, dict, str, and so on are iterative but not iterators, because the next() function cannot call them. They can be turned into iterators through the iter() function

(4) The for loop of python is essentially achieved by calling the next() function continuously

3. Generators

1. Generator Definition:

In python, the mechanism of computing while looping is called generator: generator.

Why have a generator?

All the data in the list is in memory, which can be very memory intensive if you have a lot of data.
For example, we only need access to the first few elements, but most of the memory consumed by the following elements is wasted.

The generator then continuously deduces subsequent elements from the algorithm during the loop, eliminating the need to create a complete list, which saves a lot of space.

In short, when we want to use large data and take up less space, we use generators.

2. Create Generator

(1) Generator expression: Generator expression comes from a combination of iteration and list resolution. Generators and lists are similar, but they use () instead of []. (similar to the theoretical tuple derivation)

g = (x for x in range(5))
print(g)       # generator object
print(next(g))
print(next(g))
print(next(g))
print(next(g))
print(next(g))
# Error Exceeded
print(next(g))

for i in g:
    print(i)

3. Generator functions

When a function contains the yield keyword, it is no longer a normal function but a generator. Calling a function creates a generator object. It works by calling next() or u repeatedly Next_u () method until an exception is caught.

def yieldtest(number):
    n = 0
    # li = []
    while n<number:
        # li.append(n)
        yield n
        n+=1

res = yieldtest(3)
print(res)       # generator object
print(next(res)) # 0
print(next(res)) # 1
print(next(res)) # 2

Note: yield returns a value, and remember the location of the return value. The next time you encounter a next () call, the code will start with yield's next statement. The difference from return is that return returns a value, but ends the function directly.

For example, to implement a Fibonacci sequence, any number except the first and second numbers can be added together by the first two numbers, coded as follows:

              
def createNums():
    print("-----func start-----")
    a,b = 0,1
    for i in range(5):
        # print(b)
        print("--1--")
        yield b
        print("--2--")
        a,b = b,a+b
        print("--3--")
    print("-----func end-----")
    
g = createNums()
print(next(g))  
print(next(g))  
print(next(g))
print(next(g))
print(next(g))

4.send() function

Send() and next() both allow the generator to go one step further (yieldreturn is encountered), but send() can pass a value that is the result of the entire yieldexpression.

def test():
    a1 = yield "hello"
    print("---1---")
    yield a1

res = test()
print(next(res))          # "hello"
print(res.send("world"))  # "world"

That is, the send method can force the last yield expression value to be modified.
For example, there is a yieldassignment in the function, a1 = yield "hello", where the first iteration returns "hello", but a1 has not yet been assigned. In the second iteration, using.send("world"), you would force the yield "hello" expression to be "world", so yield a1 would result in "world".

5. Iterators and Generators

(1) Generators can do everything an iterator can do

(2) Because the generator automatically creates iter() and next() methods, the generator is concise and efficient.

Tags: Python list

Posted on Mon, 06 Dec 2021 12:52:50 -0500 by tomhath