In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces how to analyze Python generators, iterators and yield statements, the content is very detailed, interested friends can refer to, hope to be helpful to you.
What we're going to share today are Python generators, iterators, and yield statements. It mainly includes what is a generator, how to define a generator, and how to call the elements contained in the generator. The same is true for iterators, and finally I introduce the yield statement and how it relates to the generator.
[*! *] to understand this article, you need to understand the definition of Python list, basic operations, dictionaries, tuples, and string concepts. The syntax structure of the for loop in Python, and you need to know
If _ name__ = = "_ _ main__":
What is the function of?
1. Iteration
Let's first take a look at the definition of iteration:
Given a list list or tuple tuple, we can traverse the list or tuple through a for loop, which we call Iteration.
To say it in human terms is to give a list or tuple, to see what the elements are one by one, and to look at it only once, it is called iteration. Write a simple chestnut below. First define a list_a, then iterate through each element through the for loop, and print out each element in the list_a (figure 1). This is the process of iterating over the list_a.
Definition of iterator:
An object that can be called by the next () function and constantly returns the next value is called an Iterator.
Generally speaking, iterators can be iterated.
Figure 1
What I just introduced is iterating over the list in Python, so can I iterate over other objects as well? How to tell whether an object is iterable or not? We can use the Iterable function in the collections module to determine whether an object is iterable or not. For example:
Define lists, tuples, strings, dictionaries, and integers as five data types, and then determine whether they can be iterated or not, as shown in figure 2. As a result, we can see that except for integers, the other four data types are iterable.
Figure 2
two。 List generation
As the name implies, list generation is used to automatically create a list expression. One of the benefits of using list generation to create lists is that you can simplify the code, make the code beautiful, and reduce the amount of work.
For a simple chestnut, we define a list, name_list, and then use list generation to convert all uppercase letters in name_list to lowercase letters. Among them
[name.lower () for name in name_list]
It is list generation, which first defines some kind of operation on the object, and then defines a for loop to traverse the object.
Figure 3
3. Generator
List generation is generally used in situations where the list is not very long and the memory consumption is relatively small. If the amount of data is large, the generator is a better choice than list generation. In Python, the mechanism of performing a loop while performing a certain operation is called a Generator. A simple way to define a generator is to change [] to () in the list generator. Or the chestnut just now. Let's print the generator object and see
Lower_name_list is a generator object, generator object.
Figure 4
Then describe how to see what elements are in this generator object? For generator objects, use the generator's next () method to output each object. Next () means next, as if next () said to the generator, come on, next, the generator spits out the next element, and throws a StopIteration exception when it knows that there are no objects to iterate over in the generator (figure 5).
Figure 5
Next, let's learn another way to output the elements in the generator, which is to iterate over the elements in the generator with a for loop (figure 6). This is because the generator is an iterable object, so you can use a for loop to traverse.
> isinstance (lower_name_list1,Iterable)
> True
Figure 6
4. Yield statement
Next to what we are going to focus on today, we have to force Amway here. Yield is very useful, and it is best to master it skillfully. And then why do you say that? I'm going to talk about deep learning for a while, what we call machine learning, or deep learning, one of the very important links is to read the data, that is, to read the data in and give it to our model to train and learn. So the question is, if I give you 1 million pictures, or 1G of text data, how do you read the data? When solving the machine learning algorithm, most of the traditional methods use the batch gradient descent method (Batch Gradient Descent). Generally, they read all the data at once, and then do the gradient descent as a whole. But now is the era of big data, the amount of data is generally very large, if read in, the memory is certainly not enough, if it is GPU, the same video memory is not enough. So there is a later decline in the mini-batch gradient, how much mini is usually far less than the total amount of data, this number is the size of the batchsize we got when we trained the model. It means that from a large data set, only a small part of the data set is taken at a time, and then traverse the entire data set.
This idea is very consistent with Python's yield statement, so I highly recommend that you master the yield statement. Let's start to learn the yield sentence.
First, let's take a look at the explanation of yield in the official Python documentation.
The yiled statement is used only when defining a generator function, and in the function body of the generator function. After using the yield statement in the function definition, this function is not a normal function, but a generator function.
When we call the generator function, we will return a generator. We execute the generator function by calling the generator's next () method until an exception is thrown.
When the yield statement is executed, the generator object is frozen, and the result of execution is only the list returned by the next () method. Freezing means that all variables will not be executed except that the next () method can return a list.
A simple understanding of this document is that we can define a generator function by defining a function that contains a yield statement. This generator function can be executed through the next () method.
Let's take a specific chestnut and take a look at the implementation principle of yield.
First, we define two functions, yield_test () and yield_test2 (). The first function returns the output value with return, and the second function returns the output value with yield. This is done to reflect the difference between return and yield, and to reflect the differences that include yield statement functions. Why do you want to make this comparison? to put it bluntly, the yield statement actually returns a value, but this return method is unusual, it is returned in the form of a generator function, so let's compare the difference with return and see what's different.
#-*-coding:utf-8-*-
Defyield_test ():
List_a = range (5)
List_b = []
For i in list_a:
List_b.append (I * I)
Return list_b
Defyield_test2 ():
List_a = range (5)
For i in list_a:
Yield I * I
If _ name__ = = "_ _ main__":
Results = yield_test ()
Results2 = yield_test2 ()
Print ("The results is:", results)
Print ("The type of results is:", type (results))
Print ("The results2 is:", results2)
Print ("The type of results2 is:", type (results2))
Result_list = []
For x in results2:
Result_list.append (x)
Print ("The result_list is:", result_list)
Here is the result of the program output
"
('The results is:', [0,1,4,9,16])
('The type of results is:',)
('The results2is:',)
('The type of results2 is:',)
('The result_list is:', [0,1,4,9,16])
"
On how to analyze the Python generator, iterator and yield statements to share here, I hope the above can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.