Python derivation and iterators, generators

Source: Internet
Author: User
Tags generator iterable

1. Common derivation formula
    • Derivation is a quick and concise way to create data structures from one or more iterators.
1.1 _ List derivation
  • Simplest form: [exprssion for item in Iterable ]
    • Example: number_list = [x for x in range (1,ten)]
  • in the form of a conditional expression:    [exprsssion for Item in iterable if condition ]  
    • Example:  a_list = [x for X " Span style= "color: #0000ff;" >in Range (1 , 7 ) if x%2 ==1 ]  
  • Multiple for nested expressions: [(x, y) for x in x_list for y in y_list] generates a list of x, y tuples.
1.2 _ Dictionary derivation type
    • Simplest form: {key_expression:value_expression for expression in Iterable }
      • Example: Word = 'letter', let_dict = {let:word.count Word}
      • The previous example has two occurrences of letters, wasting time using set to improve  let_dict = {let:word.count(let) for let in set(word)} 
1.3 _ Set Deduction formula
    • Similar to lists and dictionaries:  {expression for expression in iterable} 
1.4 _ Generator Derivation
    • The tuple has no derivation, and its parentheses are used to sit the generator derivation
      •  t_generotor = (x for x in range(1,8)) 
    • Generator can be converted to list derivation t_list = List (t_generotor)
    • A generator can run only once, and it will not run out. Lists, collection strings, and dictionaries are all stored in memory, but the generator only produces values in the run and is not stored, so you cannot reuse or back up a generator.
2. Generator
    • The builder is an object used to create a python sequence. It allows you to iterate over large sequences without having to create and store the entire sequence in memory.
    • Each time the generator is iterated, it records the location of the last call and returns the next value. This is not the same as a normal function, and the general function does not record the last call, and it will be executed at the first line of the function.
    • To create a larger sequence, the code that uses the generator derivation is very long and can write a generator function. The generator function is similar to a normal function, but its return value is declared instead of return using the yield statement.

      • def my_range (first=0, last=10, step=1): <ul><li>number = first</li> <li>while number < last:& lt;/li> <li>yield number</li> <li>number+=step

    • My_Range returns a generator object that can be iterated using this Builder object: for x in My_Range (): print x

3. Using Zip parallel iterations
    • When using iterations, multiple sequences can be iterated in parallel via zip ().
      • days= {' Monday ', ' Tuesday ', ' Wednesday '}</li> <li>fruit= {' banana ', ' orange ', ' peach '}</li> <li >dessert = {' Misc ', ' Drink ', ' ice ', ' pudding '}</li> <li>for day,fru,drink in Zip (days,fruit,dessert): </li> <li>print Day,fru,drink

    • The zip () function stops when the shortest sequence is exhausted, and the elements behind the longest list above cannot be populated
    • A dictionary can be obtained with the return value of the DITC () function and the zip () function.
      •  dict(zip(list_1,list_2)) 
4. iterators
      • 4.1 Iterator rules
        • Iteration means repeating things many times-just like in the loop.
        • __iter__method returns an iterator (iterator), the so-called iterator that has the next method (this method does not need to pass any parameters when it is called). When the next method is called, the iterator returns its next value. If the next method is called, but the iterator has no value to return, a Stopiteration exception is thrown. [3.0 is __next__ ]
        • An object that implements the __iter__ method is iterative, and an object that implements the next method is an iterator

Python derivation and iterators, 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.