Python iterator toolkit [recommended], python generator Toolkit
Original article: https://git.io/pytips
0x01 introduces the concept of iterator, that is, objects defining the _ iter _ () and _ next _ () methods, you can also use yield to simplify the definition of "iteratable objects". In some functional programming languages (see functional programming in
Background knowledge:In Python, a function is required to run, which requires three things in a Python vm.
Pycodeobject, the code that saved the function.
Pyfunctionobject, this represents a function object in a virtual machine.
Pyframeobject, which represents the call chain and the stack when the function is run
Python is the only thing tha
Yield and pythonyield of python Generator
For details, refer to [Python yield Usage Analysis] and [differences between Python xrange and range ].
A function with yield is a generator, which is different from a common function. Generating a
We can create a list simply by creating a list, but with memory limitations, the list size 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, then 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
1. 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
during next executionTo pass a value in the generator, there is an activation process, and the first time you must use next to trigger the generatorHow to take a value from the generator(1) Next can be stopped at any time, the last one will be an error(2) The For loop is traversed from beginning to end, no break is encountered, return does not stop(3) A strong turn of the list tuple data type will load all
We can create a list simply by creating a list, but with memory limitations, the list size 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, then 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 li
Python iterator and python Generator
1. yield: Convert the function into a generator (generator)
Example: Fibonacci Series
Def fib (num): a, B, c = 1, 0, 1 while a
2. Iterable
All objects that can use the for loop, collectively referred to as Iterable)
From collection
Python generator to generate the Yang Hui triangle (mandatory), python Yang Hui
It feels awkward to write interesting programs in Python.
# Generate and display the yang Hui triangle by the generator # The principle is to display the yang Hui triangle in a 2-dimension grou
1. What is a generatorWith 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 th
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 co
BackgroundBefore the concept of the Python process, the two days and a colleague chat to the process, the dead and alive can not think of what has been seen, remember a yield, the concept is unclear;So I want to smooth out the relevant things, this article as a record of learning.GeneratorGenerator (generator) is an algorithm that can be understood as a special function that has iterative ( 可迭代的对象都有一个__next
Python iterator and Python Generator
The problem is that it is generated during the loop in Python. anyone familiar with Python knows that it is not similar to the for loop in other languages and can only perform loop traversal through the for in method. The most typical app
With a list generation, we can create a list directly. However, the list capacity is certainly limited by the memory limit. And, creating a list of 1 million elements, not only takes up a lot of storage space, but if we just need to access the first few elements, the space behind most of the elements is wasted.
So if the list element can be calculated according to some algorithm, can we continue to calculate the subsequent elements in the process of the loop? This eliminates the need to create
. All the work done in the function and the data saved in the local variables will be lost. When you call this function again, everything will be created from scratch. The function has only one chance to return the result, so all results must be returned at once. Usually we all think so. But what if they are not? Please see the magic Yield:def fib (n): a=1 yield a b=1 for i in range (n-1): a,b=b,a+b yield AIF __name__== ' __main__ ': For i in fib: print i>>> 1
The builder is one of the most attractive features of the Python language, and the generator is a special kind of iterator, but it's more elegant. It does not need to write the __iter__ () and __next__ () methods as in the class above, only one yiled keyword is required.Iterate through the list of all the words in the nested list provided, and then iterate through the elements in the list sequentially. Any
ObjectiveThere is no use of things, there is no deep understanding of things difficult to say that they will, and be asked by others inevitably flawed. Although the previous contact with the concept of Python, but just a glance, the two days of a conversation, others asked the association, suddenly speechless, dead and alive to think of things that have been seen, then suddenly thought of yield, but too late, can only say the concept of unclear, So th
Python -- iterator, python -- Generator
Iterator
1. iteratable objects
Can directly act on the for loop type:
These are collectively referred to as iteration objects, Iterable
You can use isinstance () to determine whether an object is Iterable:
True >>> from collections import Iterable >>> isinstance ([], Iterable) True >>> isinstance ({}, Iterable) True >>> is
= "Nicholas Zhao Si"
print ("Hip-hop dance" if name == "Nicholas Zhao Si" else "Can't hip-hop dance") One yuan: "Hip-hop dance" Binary: Judgment by if statement Ternary: "No hip-hop dance"
Output results:
Hip-hop
In fact, it is well understood that the if front can be understood as the return result that evaluates to true, else is the return result that evaluates to False, this is the ternary operation.
Ternary operations can also be used in conjunction with list generation,
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.