How to use Python iterator correctly in Python learning notes

Time:2020-10-22

1、 Iterator correlation
Iterator, sometimes called cursor, is a software design pattern of programming. It can be accessed all over the container (such as linked list or array). Designers don’t need to care about the contents of container.

Key point: visit all over (traverse?) Container write here to recommend a full range of resources Python learning free answer Seven clothes nine seven seven bar five (Digital homophony) conversion can be found, here are senior programmers to share previous learning experience, learning notes, as well as work experience of first-line enterprises, and carefully collate a copy of Python zero foundation to project actual combat materials, explain the latest technology of python, prospects, learning need to leave a message of small details

2、 Characteristics of iterator
1. Iterators can access data from the first data in the collection to the last data

2. Iterators can only be accessed forward, not backward (next() method)

3. Two basic methods of iterator: next () and ITER ()

3、 Iterators in Python
Two basic methods of iterator: next (), ITER ()

1. Get the iterator of string type and traverse while

a, i = “Baldwin”, 0
it = iter(a)
while i < len(a):
print(next(it), end=” “)
i += 1
E:\WorkSpaces\PycharmProjects\PyDemo\venv\Scripts\python.exe E:/WorkSpaces/PycharmProjects/PyDemo/cn/yzstu/__init__.py
B a l d w i n
Process finished with exit code 0
2. For traversal

a = “Baldwin”
it = iter(a)
for a in it:
print(a,end=” “)
E:\WorkSpaces\PycharmProjects\PyDemo\venv\Scripts\python.exe E:/WorkSpaces/PycharmProjects/PyDemo/cn/yzstu/__init__.py
B a l d w i n
Process finished with exit code 0
4、 Create iterator
It is the default that the reader has an object-oriented understanding basis

If we want to use a class as an iterator, we need to implement it__ iter__ () and__ next__ () two methods.

Wei iter__ (): returns an iterator object and implements it in this iterator object__ next__ () method

Wei next__ (): in Python 2, this method is called next (), which returns the next iterator class numbersforit:

def __ iter__ (self): create__ iter__
self.a = 1
return self

def __ next__ (self): create__ next__
b = self.a
self.a += 1
return b

numbwes = NumbersForIt()
it = iter(numbwes)
print(next(it))
print(next(it))
print(next(it))
print(next(it))
print(next(it))
print(next(it))
E:\WorkSpaces\PycharmProjects\PyDemo\venv\Scripts\python.exe E:/WorkSpaces/PycharmProjects/PyDemo/cn/yzstu/__init__.py

Process finished with exit code 0
5、 The generator creates an iterator
In Python, functions that use yield are called generators.

Different from ordinary functions, the generator is a function that returns an iterator and can only be used for iterative operations. It is simpler to understand that a generator is an iterator.

In the process of calling the generator to run, every time yield is encountered, the function will pause and save all the current running information, return the value of yield, and continue to run from the current position the next time the next () method is executed.

Calling a generator function returns an iterator object.

def generator():
a = 1
while True:
yield a
a += 1

it, b = generator(), 0
while b < 10:
print(next(it), end=” “)
b += 1
E:\WorkSpaces\PycharmProjects\PyDemo\venv\Scripts\python.exe E:/WorkSpaces/PycharmProjects/PyDemo/cn/yzstu/__init__.py
1 2 3 4 5 6 7 8 9 10
Process finished with exit code 0
Using generator to realize Fibonacci sequence

def generator():
a, b = 1, 1
while True:
yield a
a, b = b, a + b

it, m = generator(), 0
while m < 10:
Print (next (it), end =) ා at this time, we get the value of a above
m += 1
E:\WorkSpaces\PycharmProjects\PyDemo\venv\Scripts\python.exe E:/WorkSpaces/PycharmProjects/PyDemo/cn/yzstu/__init__.py
1 1 2 3 5 8 13 21 34 55
Process finished with exit code 0
6、 Summary
1. The correct use of iterators is very important in any language

2. The use of yield can quickly create an iterator. It should be noted that the correct iteration rules should be added in while

Finally, I would like to recommend a full range of resources for Python learning Seven clothes nine seven seven bar five (Digital homophony) conversion can be found, here are senior programmers to share previous learning experience, learning notes, as well as work experience of first-line enterprises, and carefully collate a copy of Python zero foundation to project actual combat materials, explain the latest technology of python, prospects, learning need to leave a message of small details
The text and pictures of this article are from the Internet and my own ideas. They are for study and communication only. They do not have any commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for handling.