Python path----------generator

Source: Internet
Author: User
Tags for in range

I. list-generated

Think about how to create a list [0,1,2,3,4,5]

1 l = [0,1,2,3,4,5]

Would you write a lot of code if the list elements above are enough? See how the list is Generated.

1 # list-generated 2  for  in range (6)]34# The above code equals 5 l = []  6 for in range (6):7    l.append (x)8 9 # use list generation to save code and quickly generate lists

second, Generator (generator)

What is a generator?

With List generation, We can create a list directly. however, with memory limitations, The list capacity is certainly limited. also, creating a list of 1 million elements takes up a lot of storage space, and if we just need to access the first few elements, the vast majority of the space behind it is wasted.

so, if the list element can be calculated according to an algorithm, can we continue to calculate the subsequent elements in the process of the loop? This eliminates the need to create a complete list, which saves a lot of space. In python, this side loop computes the mechanism, called the generator (Generator).

The generator generates the corresponding data only when it is called, and the generator only records the current position and the generator has only one __next__ () method, but generally does not use the next method to call the generator with a loop.

1 Import time2A =time.time ()3L = [x forXinchRange (30000000)]4b =time.time ()5 Print(b-A)6 7A =time.time ()8g = (x forXinchRange (30000000))9b =time.time ()Ten Print(b-A) one  a  - #the list generation time can be very long and memory, while G is not time consuming with generators (because the generator call has Data)

Fibonacci sequence

Fibonacci Series (Fibonacci sequence), also known as the Golden Section series, because the mathematician Leonardo's Fibonacci (leonardoda Fibonacci) to the rabbit breeding as an example of the introduction, so called "rabbit series", refers to such a series: 1, 1, 2, 3, 5, 8, 13, 21, 、...... In mathematics, the Fibonacci sequence is defined as Follows: F (1) =1,f (2) =1, f (n) =f (n-1) +f (n-2) in the fields of modern physics, quasi-crystalline structure, chemistry, and so on, the Fibonacci sequence has a direct application, From 1963, the American Mathematical Council published a mathematical magazine in the name of the Fibonacci series quarterly, devoted to the Research.

1 def fib (max): 2     n, a, B = 0, 0, 13      while n < max:4         print(b)5          a, B = b, A + b6         n = n + 1

Generator function Version:

1 def fib (max): 2     n, a, B = 0, 0, 13      while n < max:4         yield  b5
             a, B = b, A + b6         n = n + 1

third, the use of generators

Generator Parallel computing (simple producer Consumer Model) asynchronous IO Model prototype

1 #Coding=utf-82 Import time3  fromRandomImportChoice4 5 defproducer ():6Mooncake = ["Five kernels stuffing","Salted egg yolk stuffing","black sesame Paste","Green red silk stuffing","Pork Meat","Salted Fish Stuffing"]7c = Consumer ('MR.A')8C1 = Consumer ('mis.b')9C.__next__()Tenc1.__next__() one     Print("The food factory began to produce Mooncakes.") an =0 -      whileN < 3: -Time.sleep (1) theMooncake1 =Choice (mooncake) -         Print("the food factory produced the%s mooncake"%Mooncake1) - c.send (mooncake1) -Mooncake2 =Choice (mooncake) +         Print("the food factory produced the%s mooncake"%Mooncake2) - c1.send (mooncake2) +n + = 1 a  at defConsumer (name): -     Print("%s ready to start eating moon Cakes."%Name) -      whileTrue: -Mooncake =yield -         Print("%s ate the%s mooncake"%(name, Mooncake)) -  inProducer ()

Python path----------generator

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.