Python Learning note-day03-Part III (Introduction to iterators and builds)

Source: Internet
Author: User

Iterators

An iterator is a container object that has two basic methods

Netxt () #返回容器的下一个元素

__iter__ #返回迭代器自身
Iterators have been added to Python since version 2.2, which provides a class sequence interface for class sequence objects, essentially an iterator that has an object with a next () method, rather than an index to count, and when you or a looping mechanism (such as for) needs the next item, the next of the iterator is called The () method can get to it, the entry is all taken out, or throws a Stopiteration exception, and you do not see the exception, and Python internally handles the exception to tell the external caller that the iteration is complete.
Iterators also have some limitations, such as not moving backwards, not going back to the beginning, or duplicating an iterator, if you need to iterate over the same object, only by creating another iterator object's method implementation,

A big advantage, without having to prepare all the elements in advance for the entire iteration, the iterator computes the element only when iterating to an element, before or after which the element may not exist or be destroyed. Especially useful when traversing some particularly large or infinite collections.
###################################################################################
Generator

The generator is an important feature added when python2.2, the generator is a specific function that allows you to return a value and then ' pause ' or ' suspend ' the execution of the code, and then restore it later.

Syntactically, the generator is a function with a yield statement (the function of the yield statement is to tentatively execute and return an intermediate result),

Range is not a generator and xrange is a generator
ReadLines is not a generator and xreadlines is a generator
>>> Print Range (10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print xrange (10)
Xrange (10)

The generator is created internally based on yield, which is created only when used by the generator, thus avoiding memory waste

Bubble sort
Practice:
Like the following tables:
[13, 22, 6, 99, 11]

Please follow the rules to calculate:
13 and 22 Compare, put large values on the right side, namely: [13, 22, 6, 99, 11]
22 and 6 Compare, put large values on the right side, namely: [13, 6, 22, 99, 11]
22 and 99 Compare, put large values on the right side, namely: [13, 6, 22, 99, 11]
99 and 42 Compare, put large values on the right side, namely: [13, 6, 22, 11, 99,]

13 and 6 Compare, put large values on the right side, namely: [6, 13, 22, 11, 99,]
...

Li = [13, 22, 6, 99, 11]

For M in range (Len (LI)-1):

For n in range (m+1, Len (LI)):
If Li[m]> Li[n]:
temp = Li[n]
Li[n] = Li[m]
LI[M] = Temp
Print Li




51cto Blog Address

Http://timesnotes.blog.51cto.com

http://timesnotes.blog.51cto.com/1079212/1711145

This blog address:

http://www.timesnotes.com/

http://www.timesnotes.com/?p=91


This article is from the "'ll Notes" blog, so be sure to keep this source http://timesnotes.blog.51cto.com/1079212/1711145

Python Learning note-day03-Part III (Introduction to iterators and builds)

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.