Miscellaneous talk on the use of Iterator in Python
Iterator is an object that supports next() operation. It contains a set of elements. When the next() operation is performed, one of the elements is returned; when all elements are returned, a StopIteration exception is generated.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
ite() can accept a variety of Python objects as parameters, such as list, tuple, dict, set, etc., and convert them into iterators. Iterators can be used in for statements or in statements. Many common operations also support iterators, such as sum(), max(), etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Needless to say, iterators have many benefits:
1. "Streaming" data processing method reduces memory consumption:
For example, when processing a file, suddenly taking out all the data and putting it into the memory for processing will cause the program to consume a lot of memory, and sometimes it is even impossible to do. Generally, we will process the file content part by part:
1 2 |
|
2. Or when processing xml files:
1 2 3 4 5 6 |
|
The file object returned by the built-in function open and the xml tree serialized by etree.iterparse are both iterable objects, which allow us to progressively process the contents of the file.
3. Supports the convenience of using for statements to consume data:
Some common built-in types in Python such as arrays, lists and even strings are iterable types, so that we can use the syntax sugar of the for statement to conveniently consume data without having to record the index position ourselves. Human loops :
1 2 |
|
After briefly understanding the benefits of iterators, let’s talk about python’s iterator mode in a serious way.
Here we introduce two more convoluted terms: iterable objects and iterator objects. Personally, I feel that starting from these two concepts will give a better understanding of iterators. Before giving examples, let’s give a rough explanation of these two concepts:
Iterable object: The object contains the implementation of the __iter()__ method. After the iter function of the object is called, it will return an iterator, which contains the implementation of specific data acquisition.
Iterator: Contains the implementation of the next method, returns the expected data within the correct range, and can throw a StopIteration error to stop iteration after exceeding the range.
Give me an example and say it while reading:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
The iterable_range in the example is an iterable object, so we can also use the for statement to iterate over it:
1 2 3 |
|
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
You can take a closer look at the output log:
- The data is indeed “streamed”
- Iterator is the person who really does the work behind the scenes
- The for statement can iterate the data of an object very conveniently.
Iterable objects are actually more like the upper layer of the entire iterator pattern, like a constraint, a contract, and a specification. It can ensure that it can return an iterator object that does actual work. Methods such as for and sum that accept an iterable object all follow this specification: call the __iter__ function of the object, return the iterator, process each value returned by the iterator object, or require some summary operations. Take for as an example:
1 2 3 4 5 6 7 8 9 10 |
|
for is almost as shown in the code in the above example: first get the iterator object returned by the iterable object, and then call the next method of the iterator object to get each value. In the process of getting the value Detect the boundary at any time - that is, check whether an error such as StopIteration is thrown. If the iterator object throws an error, the iteration stops (note: As can be seen from this example, for those methods that accept iterable objects, if we pass an In fact, a simple iterator object cannot work, and an error similar to TypeError: iteration over non-sequence may be reported).
Of course, generally we will not separate them intentionally during the application process. We can slightly modify the iterator object and add the implementation of the __iter__ method, so that the object itself is both an iterable object and an iterator object. :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.
