Python function Programming Guide (3): iterator

Source: Internet
Author: User
Tags imap iterable

This article will discuss the iterator. The iterator is not unique to functional programming, but it is still an important component of functional programming, or an important tool.

For reprint, please specify the original author and original address :)

3. iterator 3.1. iterator (iterator) Overview

An iterator is a way to access elements in a set. The iterator object is accessed from the first element of the set until all elements are accessed.

The iterator cannot be rolled back and can only be iterated forward. This is not a big drawback, because people almost do not need to perform rollback during iteration.

The iterator is NOT thread-safe. It is a dangerous operation to use the iterator for variable sets in a multi-threaded environment. However, if you are cautious, or simply stick to the immutable set by implementing functional thinking, that is not a big problem.

For data structures (such as tuple and list) that support random access in the native, The iterator has no advantage over index access in the classic for loop, instead, the index value is lost (you can use the built-in function enumerate () to retrieve the index value ). However, for data structures (such as set) that cannot be accessed randomly, the iterator is the only way to access elements.

Another advantage of the iterator is that it does not require you to prepare all the elements in the entire iteration process in advance. The iterator calculates the element only when it iterates to an element. Before or after this, the element may not exist or be destroyed. This feature makes it especially suitable for Traversing large or infinite sets, such as several g files or Fibonacci series. This feature is called lazy evaluation ).

The primary benefit of the iterator is that it provides a unified interface for accessing the set. You can use the iterator to access the object that implements the _ ITER _ () method.

3.2. Use the iterator

You can use the built-in factory function ITER (iterable) to obtain the iterator object:

>>> lst = range(2)>>> it = iter(lst)>>> it<listiterator object at 0x00BB62F0>

You can use the next () method of the iterator to access the next element:

>>> it.next()0

For Python 2.6 +, there is also the built-in function next (iterator) to complete this function:

>>> next(it) 1

How can we determine whether the iterator has more elements to access? The iterator in Python does not provide methods like has_next.

In this example, we have accessed the last element 1. What if we use the next () method?

>>> it.next() Traceback (most recent call last):   File "<stdin>", line 1, in <module> StopIteration 

When Python encounters such a situation, it will throw a stopiteration exception. In fact, Python determines whether to stop iteration based on whether this exception is detected.

This method has advantages over manual checks before iteration. However, python is always suspected of using exceptions for process control.

After learning about these situations, we can use the iterator to traverse them.

it = iter(lst)try:    while True:        val = it.next()        print valexcept StopIteration:    pass

In fact, because iterative operations are so common, python uses the keyword for as the syntactic sugar of the iterator. In the for loop, python will automatically call the factory function ITER () to obtain the iterator, call next () to obtain the element, and check for stopiteration exceptions. The above code can be written as follows, so you must be familiar with it:

for val in lst:    print val

First, python will call the ITER function for the object after the keyword in to obtain the iterator, and then call the next method of the iterator to obtain the element until a stopiteration exception is thrown. When the ITER function is called for the iterator, The iterator itself is returned. Therefore, the iterator can also be used in the for statement without special processing.

Several commonly used built-in data structures, such as tuple, list, set, and dict, support iterators. character strings can also be iterated. You can also implement an iterator by yourself. As mentioned above, you only need to return an object in the _ ITER _ method of the class. This object has a next () method, this method can throw a stopiteration exception when appropriate. However, there is not much time to implement the iterator by yourself. It is easier to use the generator even if needed. In the next article, we will discuss the generator section.

* Exceptions are not non-Throws. The iterator that does not throw the exception performs infinite iterations. In some cases, such an iterator is useful. In this case, you need to determine the element and stop it. Otherwise, it will be an endless loop!

Using the iterator loop can avoid indexes, but sometimes we still need indexes for some operations. At this time, the built-in function enumerate will be used. It can add an index before the result of the ITER function and return it as a tuples. It is used like this:

for idx, ele in enumerate(lst):    print idx, ele
3.3. Generator expression and list Comprehension)

In most cases, traversing a set is to apply an action to an element or filter it. If you have read the second part of this article, you should remember that the built-in functions map and filter provide these functions, but Python still provides language-level support for these operations.

(X + 1 for X in lst) # generator expression, which returns the iterator. The external brackets can be omitted when used as parameters. [X + 1 for X in lst] # list parsing, returns list

As you can see, generator expressions and list parsing (Note: there are many types of translation here, such as list expansion and list derivation, which refer to the same meaning) have little difference, when people mention this feature, it is often described as list parsing only for the sake of simplicity. However, since the returned iterator does not calculate all elements at the beginning, this gives more flexibility and avoids many unnecessary computations, therefore, unless you explicitly want to return the list, you should always use the generator expression. In the following text, I will not distinguish these two forms :)

You can also provide the if clause for list parsing for filtering:

(x+1 for x in lst if x!=0) 

Or multiple for clauses can be provided for nested loops. the nesting order is the order of for clauses:

((x, y) for x in range(3) for y in range(x))

List Parsing is a clear pythonic. I often encounter two problems of using list resolution, which should belong to the best practice, but these two problems are very typical, so I would like to mention them here:

The first problem is that, because the action on the element application is too complex to be written using an expression, list resolution is not used. This is a typical example of no change in thinking. If we encapsulate an action into a function, isn't it an expression?

The second problem is that, because the conditions in the IF clause need to be calculated, and the results also need to be calculated twice, as shown in the following figure:

(x.doSomething() for x in lst if x.doSomething()>0)

This write is really bad, but it can be solved by combining the list parsing:

(x for x in (y.doSomething() for y in lst) if x>0)

The internal list parsing variable can also use X, but for clarity, we changed to y. Or, you can write two expressions:

tmp = (x.doSomething() for x in lst)(x for x in tmp if x > 0)

List parsing can replace the vast majority of scenarios where map and filter are needed. For this reason, the famous static check tool pylint uses the columns of MAP and filter as warnings.

3.4. Related Libraries

Python has a built-in module itertools, which contains many functions for creating iterators for efficient looping (creating a more efficient loop iterator), which means it is very domineering, in this section, you can browse these functions and leave an impression on them. When you need these functions, just remember them. The content of this section is translated from the official documentation of the itertools module.

3.4.1. Unlimited Iteration
  • Count (START, [STEP])

    Start from start and add step to each element later. The default value of step is 1.

    Count (10) --> 10 11 12 13 14...

  • Cycle (P)

    After iteration to the last element of the sequence P, start from the first element of P again.

    Cycle ('abc') --> a B c d a B c d...

  • Repeat (ELEM [, N])

    Repeat ELEM n times. If n is not specified, there are no duplicates.

    Repeat (10, 3) --> 10 10 10

3.4.2. Stop iteration at the end of the shortest sequence Parameter
  • Chain (p, q ,...)

    After iteration to the last element of the P sequence, start from the first element of Q until all sequences are terminated.

    Chain ('abc', 'def ') --> A B C D E F

  • Compress (data, selectors)

    If bool (selectors [N]) is true, next () returns data [N]; otherwise, data [N] is skipped.

    Compress ('abcdef', [,]) --> a c e f

  • Dropwhile (Pred, SEQ)

    Iteration starts only when Pred calls seq [N] returns false.

    Dropwhile (lambda X: x <5, [, 1]) --> 6 4 1

  • Takewhile (Pred, SEQ)

    The opposite version of dropwhile.

    Takewhile (lambda X: x <5, [, 1]) --> 1 4

  • Ifilter (Pred, SEQ)

    The iterator version of the built-in function filter.

    Ifilter (lambda X: X % 2, range (10) --> 1 3 5 7 9

  • Ifilterfalse (Pred, SEQ)

    The opposite version of ifilter.

    Ifilterfalse (lambda X: X % 2, range (10) --> 0 2 4 6 8

  • IMAP (func, p, q ,...)

    The version of the iterator for the built-in Function Map.

    IMAP (pow, (2, 3, 10), (5, 2, 3) --> 32 9 1000

  • Starmap (func, SEQ)

    Call func as a variable-length parameter (* ARGs) for each element of seq.

    Starmap (pow, [(2, 5), (3, 2), (10, 3)]) --> 32 9 1000

  • Izip (p, q ,...)

    The version of the built-in function zip iterator.

    Izip ('abcd', 'xy') --> ax

  • Izip_longest (p, q,..., fillvalue = none)

    The version of the izip that obtains the longest sequence. The short sequence is filled with fillvalue.

    Izip_longest ('abcd', 'xy', fillvalue = '-') --> ax by c-d-

  • Tee (it, n)

    Returns the replica iterator of N iterators it.
  • Groupby (iterable [, keyfunc])

    This function is similar to SQL grouping. Before using groupby, use the same keyfunc to sort iterable, such as calling the built-in sorted function. Then, groupby returns the iterator. The elements of each iteration are tuples (key values, subiterators of the set of elements with the same key value in iterable ). Maybe it is helpful to understand this function by looking at the python sorting guide.

    Groupby ([0, 0, 0, 1, 1, 1, 2, 2]) --> (0, (0 0 0) (1, (1 1) (2, (2 2 2 ))

3.4.3. Combined iterator
  • Product (p, q,... [Repeat = 1])

    Cartesian product.

    Product ('abcd', repeat = 2) --> aa AB AC ad Ba BB BC BD ca CB CC CD da dB DC dd

  • Permutations (P [, R])

    Remove repeated elements.

    Permutations ('abcd', 2) --> AB AC ad Ba BC BD ca CB CD da dB DC

  • Combinations (P, R)

    Remove repeated elements after sorting.

    Combinations ('abcd', 2) --> AB AC ad BC BD CD

  • Combinations_with_replacement ()

    After sorting, it contains repeated elements.

    Combinations_with_replacement ('abcd', 2) --> aa AB AC ad BB BC BD CC CD dd

This article ends.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.