Python path: iterators and yield generators

Source: Internet
Author: User
Tags python list

One, iterator

For the Python list's for loop, his internal principle: to see if the next element exists, if it exists, then remove it, and if it does not, stopiteration the exception. (Python internal to exception has been handled)

one of the obvious benefits of using iterators is that you only read one piece of data from the object at a time, without causing too much memory overhead .

For example, to read the contents of a file row by line, using the ReadLines () method, we can write:

For line in open ("Test.txt"). ReadLines ():
Print Line

This can work, but not the best way. Because he was actually loading the file into memory at once and then printing it one line at a time. This method has a large memory cost when the file is large.

Using the file iterator, we can write:
For line in open ("Test.txt"): #use file iterators
Print Line

This is the simplest and fastest-running notation, and he does not read the file explicitly, but instead uses the iterator to read one row at a time.

Second, generator

Range is not a generator and xrange is a generator

ReadLines is not a generator and xreadlines is a generator

>>> print Range [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> print xrange (Ten) xrange (10)

Within the generator, based on yield creation,

PS: Created for builders only when used, thus avoiding memory waste

Practice:
such as the following table: [13, 22, 6, 99, 11] Follow the rules: 13 and 22 Compare, put large values on the right side, namely: [13,22,99,11,88,44]22 and 99 Compare, place large values on the right side, i.e.: [13,22,99,11,88,44]99 and 11 comparison, put the big value on the right side, namely: [13,22,99,11,88,44]
etc...
Li = [13,22,99,11,88,44]

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

The yield keyword is used to define the generator (Generator), and its specific function is to return a value from the function when return is used , the difference is that after yield is returned, the function can be executed from the place where the last yield returned. That is, yield returns a function, gives the caller a return value, and then "teleport" back, allowing the function to continue running until the yield statement is startled to return a new value

(The pool of links used to make the database)

Links: http://www.cnblogs.com/wupeiqi/articles/4911365.html Wu sir

Python path: iterators and yield 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.