Python path -13-iterator

Source: Internet
Author: User

13.1 Iterative Objects

We already know that there are several types of data that can be directly applied to A For loop:

A class is a collection of data types, such as list, tuple, dict, set, str, and so on;

A class is generator, including generators and generator function with yield.

These objects that can directly act on A for loop are called an iterative object :iterable.

You can use isinstance () to determine whether an object is a iterable object

#! Author:lanhan
from Collections Import iterable
Print ("list", isinstance ([],iterable))
Print ("dictionary", isinstance ({},iterable))
Print ("string", Isinstance (' abc ', iterable))
Print ("iterator", Isinstance ((x for x in range ), iterable))
Print ("digital", Isinstance (100,iterable))

13.2 iterators

An object that is called by the next () function and constantly returns the next value is called an iterator:Iterator

You can use isinstance () to determine whether an object is a Iterator object

#! Author:lanhan
from Collections Import Iterator
Print ("list", isinstance ([],iterator))
Print ("dictionary", isinstance ({},iterator))
Print ("string", Isinstance (' abc ', Iterator))
Print ("iterator", Isinstance ((x for x in range ), Iterator))
Print ("digital", Isinstance (100,iterator))

Note: The Python iterator object represents a data stream, and the iterator object can be called by the next () function and continuously return to the next data until the Stopiteration error is thrown when no data is available. This data stream can be viewed as an ordered sequence, but we cannot know the length of the sequence in advance, but we will only continue to calculate the next data on demand through the next () function, so the calculation of iterator is inert and is only calculated when the next data needs to be returned.

Iterator can even represent an infinitely large stream of data, such as the whole natural number. Using list is never possible to store all natural numbers.

Summarize:

1. The generator must be an iterator, and the iterator is not necessarily the generator

2. All objects that can be used for a for loop are iterable types, and generators are iterator objects;

3. All objects that can be used for the next () function are iterator types, which represent a sequence of lazy computations

The For loop of the example 1:python is essentially implemented by calling the next () function continuously.

For x in [1, 2, 3, 4, 5]:

Pass

is actually exactly equivalent to:

# Get the Iterator object first:

it = iter ([1, 2, 3, 4, 5])

Cycle

While True:

Try

# Get the next value:

x = Next (IT)

Except stopiteration:

# exit the loop when you encounter stopiteration

Break

4. Collection data types such as list, dict, str, etc. are iterable but not iterator, but a iterator object can be obtained through the ITER () function

13.3 iter () function

Change the list, dict, str, etc. iterable to iterator can use the iter () function

#! Author:lanhan
from Collections Import Iterator
Print ("list", Isinstance (ITER ([]), Iterator))
Print ("dictionary", Isinstance (ITER ({}), Iterator))
Print ("string", Isinstance (ITER (' abc '), Iterator))
Print ("iterator", Isinstance ((x for x in range ), Iterator))

Python path -13-iterator

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.