2018-06-19-python full stack development day18-iterators and ternary operations

Source: Internet
Author: User
Tags for in range

1. Iterator protocol

The object provides a. Next () method that executes the next method either returning the next item of the program, or getting an exception program, and this iteration can only be done backwards (an inappropriate metaphor: Only the father has a son, not a son to give birth to a father)

1.1 Iterative objects

Previous iterations of list, tuple, dict, and so on are all wrong

An iterative object is an object that satisfies an iterator protocol

Mechanism of the 2.for cycle

In the previous cognition, the For loop is followed by an iterative object, in fact for the list, dict these, the for loop does the thing, is to add a __iter__ () method behind them

  

s1=[1,2,3,4,] for in S1:# actually the internal approach is, s1.__iter__ (), convert list S1 to an iterative object So that it follows the iterator protocol and can then execute the next method

With the Iter method, the list becomes an iterative object that supports the iterator protocol, and the next method can be used

s1=[1,2,3,4] for in S1:    print(i)print(S1.  __iter__(). __next__ ())---# After using the next method, iterate, using the next method continuously until an exception is obtained, and the iterator ends. 

3. Why Use iterators

The benefit of iterators: The iterator makes the data into memory for each time it is next. If it is a list, a large amount of memory to go into memory, the data is more likely to cause the crash

Like what:

The sum of the list (range (1000000)) is calculated and the data needs to be put into memory at once.

In the case of iterators, using one data at a time, the first 1+2, the second 3+3, one analogy, may reduce the use of memory.

4. Ternary operation

Ternary operations can be thought of as a simpler list, with individuals feeling like lambda, writing easily, and reducing the number of code

  

s1=['sb'ifelse'xuzheng']  Print(S1)

4.1 List parsing

The following analysis, under normal circumstances:

  

S1=[] for in range (+):    s1.append (i)print(S1)

List resolution:

  

 for  in range (+)]print(S1)

Judging and stitching for loops

  

 for inch if i > 5]print(S1)

This is the current understanding of iterators and ternary operations, and tomorrow prepares the main introduction of the generator.

2018-06-19-python full stack development day18-iterators and ternary operations

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.