Python Builder & Iterator

Source: Internet
Author: User
Tags function definition iterable

List-Generated

To generate [1x1, 2x2, 3x3, ..., 10x10]
>>> [x * x for x in range (1, 11)]
The For loop can also be followed by an if judgment
>>> [x * x for x in range (1, one) if x% 2 = = 0]
[4, 16, 36, 64, 100]

You can also use the two-layer loop to generate a full array:
>>> [M + N for m in ' ABC ' for n ' XYZ ']
[' AX ', ' AY ', ' AZ ', ' BX ', ' by ', ' BZ ', ' CX ', ' CY ', ' CZ ']

>>> d = {' X ': ' A ', ' y ': ' B ', ' z ': ' C '} / strong>
>>> for K, V in D.items ():

turn all the strings in a list to lowercase:
>>> L = [' Hello ', ' World ', ' IBM ', ' Apple ']
>>> [S.lower () for S in L]
[' hello ', ' World ', ' IBM ', ' Apple ']

generator: Generator
The first method, as long as a list-generated [] is changed to (), a generator is created:
>>> L = [x * x for x in range]
>>> L
[0, 1, 4, 9, -------------------------]
>>> g = (x * X for x in range (Ten)
>>> g
<generator object <genexpr> at 0x1022ef630>
The difference between creating L and G is only the outermost [] and (), L is a list, and G is a generator.

get the next return value for generator with the next () function:
>>> Next (g)
0
>>> Next (g)
1
>>> Next (g)
4
use a For loop because generator is also an iterative object:
>>> g = (x * x for x in range )
>>> for N in G:
... print (n)

/////////////////a, B = B, A + B (Assignment statement)
a different approach
a function definition contains the yield keyword, then this function is no longer a normal function, but a generator
def fib (max):
N, a, b = 0, 0, 1
While N < max:
yield b
A, B = B, a + b
n = n + 1
return ' Done '
The function that becomes generator, executes at each call to next (), encounters a yield statement return, and executes again from the yield statement that was last returned.

Iterators
An object that can be called by the next () function and continually returns the next value is called an iterator: Iterator
Generators are iterator objects, but list, dict, and Str are iterable, but not iterator

To change the list, dict, str, etc. iterable to iterator you can use the ITER () function:
>>> Isinstance (ITER ([]), Iterator)
True
All objects that can be used for a for loop are iterable types;
All objects that can be used for the next () function are iterator types, which represent a sequence of lazy computations

Python Builder & iterator

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.