Home Backend Development Python Tutorial Miscellaneous talk on the use of Iterator in Python

Miscellaneous talk on the use of Iterator in Python

Jul 21, 2016 pm 02:53 PM
python Iterator

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

>>>a=[1,2,3]

>>>ia=iter(a)

>>>next(ia)

1

>>>next(ia)

2

>>>next(ia)

3

>>>next(ia)

Traceback (most recent call last):

 File "<stdin>", line 1, in <module>

StopIteration

Copy after login

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

>>> b=[4,5,6]

>>> ib=iter(b)

>>> for x in ib:

...   print(x)

...

4

5

6

>>> ic=iter(b)

>>> sum(ic)

15

>>> id=iter(b)

>>> max(ic)

6

Copy after login

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

for text_line in open("xx.txt"):

 print text_line

Copy after login

2. Or when processing xml files:

1

2

3

4

5

6

tree = etree.iterparse(xml, ['start', 'end'])

for event, elem in tree:

  if event == "end"

    result = etree.tostring(elem)

    elem.clear()

    print result

Copy after login

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

for i in [1, 2, 3, 4]

 print i,

Copy after login

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

class iterable_range:

  def __init__(self, n):

    self.n = n

 

  def __iter__(self):

    return my_range_iterator(self.n)

 

class my_range_iterator:

  def __init__(self, n):

    self.i = 0

    self.n = n

 

  def next(self):

    if self.i < self.n:

      i = self.i

      self.i += 1

      print 'iterator get number:', i

      return i

    else:

      raise StopIteration()

Copy after login

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

temp = my_range(10)

for item in temp:

  print item,

Copy after login

Output:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

my iterator get number: 0

0

my iterator get number: 1

1

my iterator get number: 2

2

my iterator get number: 3

3

my iterator get number: 4

4

my iterator get number: 5

5

my iterator get number: 6

6

my iterator get number: 7

7

my iterator get number: 8

8

my iterator get number: 9

9

Copy after login
Copy after login

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

iterator_object = iterable_object.__iter__()

while True:

  try:

    value = iterator_object.next()

  except StopIteration:

    # StopIteration exception is raised after last element

    break

 

  # loop code

  print value

Copy after login

The logic behind the syntax sugar of

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

class my_range_iterator:

   def __init__(self, n):

    self.i = 0

    self.n = n

 

   def __iter__(self):

    return self

 

   def next(self):

    if self.i < self.n:

      i = self.i

 

      self.i += 1

      print 'my iterator get number:', i

      return i

    else:

      raise StopIteration()

 

 for item in my_range_iterator(10):

   print item

Copy after login

Output:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

my iterator get number: 0

0

my iterator get number: 1

1

my iterator get number: 2

2

my iterator get number: 3

3

my iterator get number: 4

4

my iterator get number: 5

5

my iterator get number: 6

6

my iterator get number: 7

7

my iterator get number: 8

8

my iterator get number: 9

9

Copy after login
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

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.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

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 vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

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.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

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.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

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.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

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.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

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 and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

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.

See all articles