python basis (eight): function generator and yield iterator iter

yield Builder:

  • yield a generator that generates an iterator, its role is to become a function of a generator.

Different from ordinary functions, a generator is returned iterator functions only for an iterative operation. During the call generator in every encounter yield function will pause and save the current memory to run all of the information, the return value of yield, and continue to operate from its current location once the next.

NOTE: may be circulated through or manually invoked for Xun (5) of the next () method.

Example 1:

When a return, i.e. functions running off to the return, the return value.

def getNum(n):
    i = 0
    while i <= n:
        return i        # 循环结束,不能再运行下一行
        i += 1
print(getNum(5))
# output:0

The return into the yield:

def getNum(n):
    i = 0
    while i <= n:
        yield i
        i += 1
print(getNum(5))
# output:<generator object getNum at 0x000002D818C496D8>

 It returns a generator object.

The getNum (5) assigned to a, then print (a):

def getNum(n):
    i = 0
    while i <= n:
        yield i
        i += 1
a = getNum(5)
print(next(a))          # output:0
print(next(a))          # output:1

Can be seen, the first print (next (a)) is called getNum generator, when running to yield, returns the value of i (0); the second print (next (a)) that the generator then yield from running, then the operation returns the value of i (1) to yield.

In general, we use a for loop:

def getNum(n):
    i = 0
    while i <= n:
        yield i
        i += 1
a = getNum(5)
for i in a:
    print(i)

NOTE: example, when i runs to 6, i.e. 6 times a call next (a), will be given.

  • Using generators can achieve the effect of delaying the operation, called the delay means that the operation will yield results only when needed, rather than produce immediate results, saving resource consumption. And declare a different sequence, the generator is almost does not occupy memory when not in use.

Example 2:

The for loop is itself a generator. When we run the code shown below, very much memory, the card will be.

a = [x for x in range(10000000)]
print(a)

Stated another way, it becomes Builder:

a = (x for x in range(10000000))
print(a)

With about a generation, does not account for memory. (Brackets here and Ganso does not matter) 

 

Example 3:

We yield to the assignment:

def getNum(n):
    i = 0
    while i <= n:
        temp = yield i
        print(temp)
        i += 1
a = getNum(5) 
print(next(a))
print(next(a))

Will find that output is:

0
None
1

Because the yield is the return value, is running again, just run temp =, so temp is None.

We next read a.send:

def getNum(n):
    i = 0
    while i <= n:
        temp = yield i
        print(temp)
        i += 1
a = getNum(5) 
print(next(a))
print(a.send('我是a'))

Output:

0
我是a
1

With send, we "I am a" string back to the generator, assigned to the temp.

 

iterator iter:

        Iterator python is one of the most powerful features, is a way to access the collection elements.

       Iterator can remember the object is a traverse position. Iterator object began a visit from the first element of the collection, only to finish all of the elements are accessed over.

       Iterator can only move forward not backward. Iterator has two basic methods: iter (), and next ().

       Strings, lists, or Ganso, a collection of objects can be used to create an iterator:

list1 = [1, 2, 3, 4]
it = iter(list1)        # 创建迭代器对象
print(next(it))        # 输出迭代器的下一个元素 output:1
print(next(it))        # output:2

        Iterables : be in convenient object can be called iterables use for. Such as: list, string, dict.

        Iterator : it may be next () function call, and continue to return an object at a value called iterators.

        Anything that can be used with the next () is a function of the object may be an object iteration.

Guess you like

Origin blog.csdn.net/qq_26271435/article/details/89707202