Python path--day11---iterators and generators

Source: Internet
Author: User

Iteration:

An iteration is a repeating process that repeats one iteration at a time, and the result of each iteration is the initial value of the next iteration

Why you have iterators:

The values of data types, strings, lists, tuples depend on the index can be values, but dictionaries, collections, files These data types cannot be indexed by the value,

The iterator provides a method that does not rely on index values, which is an iterator

Can iterate over objects

Built-in with the __iter__ method, executing this method will get the iterator

strings, lists, tuples, dictionaries, collections, files are objects that can be iterated over

Iterator Object

Built-in with the __next__ method, executing this method will get a value from the iterator object
There is a built-in __iter__ method that executes this method to get the iterator itself

File is an iterator object

Note: An iterator object must be an iterative object, and an iterator object is not necessarily an iterator object

Use of iterators

Dic={' A ': 1, ' B ': 2, ' C ': 3}

The result of iter_dic=dic.__iter__ () #可迭代对象执行_ _iter_ _ method is the iterator object

#迭代器对象内置有_ _iter_ _ Method And _ _next_ _ method

Iter_dic.__iter__ () is Iter_dic #True iterator. __iter__ () is still the iterator itself

Print (iter_dic.__next__ ())

Print (iter_dic.__next__ ())

Print (iter_dic.__next__ ()) #等同于next (Iter_dic)

#迭代对象有几个值就需要执行几次, until no value is desirable

Print (iter_dic.__next__ ()) #抛出异常StopIteration, flags the value of the iteration object is all taken out

#有了迭代器, we can not rely on the index iteration to take the value

ITER_DIC=DIC.__ITER__ ()

While 1:

Try

K=next (Iter_dic)

Print (Dic[k])

Except stopiteration:

    Break

====================================================================

Exception handling

You can use the Try/except statement to catch an exception.

The try/except statement is used to detect errors in a try statement block, allowing the except statement to catch exception information and handle it.

If you do not want to end your program when an exception occurs, simply capture it in a try.

Grammar:

Try :< statements >        # Run other code except < name >:< statement >        # if the ' name ' exception is thrown in the try section except < name >,< data >:< statement >        # if the ' name ' exception is thrown, get additional data Else :< statement >        # If no exception occurs

================================================================================

For loop

Based on the For loop, we can implement non-dependent index values

1 dic={'a': 1,'b': 2,'c' : 3}2 for in dic:3     print( DIC[K]

How the For Loop works

1, executes the _ _iter_ _ () method of the in object to get an iterator object

2, Execute _ _next_ _ method, assign a value variable to the resulting value, and then execute the Loop body code

3, repeat procedure 2 until the exception is caught stopiteration

Advantages and disadvantages of iterators

Pros: Provides an iterative approach that does not rely on indexes

Disadvantage: Unable to obtain length

Can only be taken back, unable to move forward

Python path--day11---iterators and generators

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.