What is a
generator Although the process of creating a Python iterator is powerful, it is often inconvenient to use. The generator is a simple way to complete the iteration. Simply put, Python's generator is a function that returns an iterable object.
How to create a generator
Using the yield keyword in a general function can realize a simplest generator, and this function becomes a generator function at this time. Yield and return return the same value. The difference is that after return returns, the function state terminates, and yield will save the current function execution state. After returning, the function returns to the previously saved state to continue execution.
The difference between
generator function and general function
There are a few differences:
The generator function contains one or more yields
When the generator function is called, the function will return an object, but it will not be executed immediately
Methods like __iter__() and __next__() are automatically implemented, so we can iterate through the next() method
Once the function is yielded, the function is suspended and control returns to the caller
Local variables and their state will be saved until the next call
When the function terminates, StopIteraion will be automatically thrown
Examples:
# Simple generator function
def my_gen():
n=1
print("first")
# yield area
yield n
n+=1
print("second")
yield n
n+=1
print("third")
yield n
a=my_gen()
print("next method:")
# Each time a is called, the function is executed from the previously saved state
print(next(a))
print(next(a))
print(next(a))
print("for loop:")
# Equivalent to calling next
b=my_gen()
for elem in my_gen():
print(elem)
Use a loop generator
# Yield out the elements of the object in reverse order
def rev_str(my_str):
length=len(my_str)
for i in range(length-1,-1,-1):
yield my_str[i]
for char in rev_str("hello"):
print(char)
Generator expression
In Python, there is a list generation method, such as
# Generate a list of 1,2,3,4,5
[x for x in range(5)]
If it is replaced by [] and (), it will become the expression of the generator.
(x for x in range(5))
Specific usage:
a=(x for x in range(10))
b=[x for x in range(10)]
# This is wrong because the generator cannot directly give the length
# print("length a:",len(a))
# Length of output list
print("length b:",len(b))
b=iter(b)
# Both outputs are equivalent, but b is to open up memory at runtime, and a is to open up memory directly
print(next(a))
print(next(b))
Why use a generator
Easier to use, less code
Memory usage is more efficient. For example, the list allocates all the memory space when it is created, and the generator is only used when needed, more like a record
Represents an infinite stream. If we want to read and use more content than memory, but need to process all the content in the stream, then the generator is a good choice, for example, you can let the generator return to the current processing state, because it can Save the state, then you can process it directly next time.
Pipeline generator. Suppose we have a fast food record. The 4 lines of this record record the number of foods sold per hour in the past five years, and we need to add all the quantities together to find the total number of sales in the past 5 years. Suppose all data is a string, and unavailable numbers are marked as N/A. Then you can use the following way to deal with:
with open('sells.log') as file:
pizza_col = (line[3] for line in file)
per_hour = (int(x) for x in pizza_col if x !='N/A') # use generator for automatic iteration
print("Total pizzas sold = ",sum(per_hour))
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.