Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python containers, iterated objects, iterators, and generators are used in this way

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "Python container, iterable object, iterator and generator so application", the article explains the content is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "Python container, iterable object, iterator and generator so application" it!

1. Container 1. What is a container?

In Python, a container is a data structure that organizes multiple elements together, and the elements in the container can be iterated one by one. To put it bluntly, it works just like its name: to store things (data).

The container does not actually exist, it is not a data type, it is just a man-made concept, just to facilitate the learning of a concept word, it can use the membership operator (in or not in) to determine whether the object is in the container.

Of course, I didn't create it, I didn't have that great ability, it was officially created, well, you don't have to worry that I'm teaching you some strange nouns that no one else can understand. That's what they call it in python. Common container types are list (list), tuple (tuple), string (str), dictionary (dict), and collection (set).

Since the data in the container can be obtained iteratively, we have to learn a new concept: iterable objects.

Second, iterable object 1. What is an iterable object?

In python, an iterable object does not refer to a specific data type, it refers to a container object that stores elements.

That is, if there is no data stored in the container, it is not an iterable object, not all containers are iterable objects, and the container contains, but is not limited to, iterable objects.

Note two points:

1. Many containers are iterable objects (containers contain iterable objects). two。 An iterable object can not be iterated independently, the iteration is done through for, and all iterable objects can be accessed directly using for loop.

The for cycle is no stranger to you, is it? Have you ever wondered how it is implemented inside the for loop? For example, in the case of the for loop, why can you output every element in the list? How is its interior realized?

In fact, the for loop does two things:

1. Use _ _ iter__ () to return an iterator. The iterator will talk about it below. Let's first know that there is such a thing. two。 Use _ _ next__ () to get each element in the iterator.

So we don't use a for loop to output every element in the list.

L = [1BI 2 3 for i in 4] # for i in lvv # print (I) ite = l.recipe iterator _ () # receive what ietr () did print (ite) # print print (ite.__next__ ()) # for step 1 print (ite.__next__ ()) # step 2 print (ite.__next__ ()) when the for loop does the second thing ) # step 3 when the for loop does the second thing print (ite.__next__ ()) # for step 4 when the loop does the second thing

Output result:

As you can see, if we remove which line of code that prints ite, the execution effect is the same as every element in the output list of the for loop. The scope of the for loop is limited to 4 times, and it is actually executed 1 time _ iter__ () and 4 times _ next__ (), that is, the essence of for loop access iterative object is achieved in this way.

Moreover, the two things that the for loop essentially does are indispensable, that is, if _ _ iter__ () returns the iterator first, _ _ next () _ _ cannot get the element, which precisely illustrates the second of the two points mentioned above: an iterable object cannot be iterated independently.

There are two built-in functions with the same principle and the same essence. Generally speaking, it is more convenient to use built-in functions, at least not to write so many underscores:

The essence of the built-in function iter () is _ _ inter__ (), which also returns an iterator. The essence of the built-in function next () is _ _ next__ (), which also gets the element with an iterator.

You can see that the result is exactly the same, since we are talking about iterators, let's take a look at what an iterator is.

3. Iterator

We can probably see from the example of the for loop above.

Any object that implements _ _ iter__ () and _ _ next__ () is an iterator, and an iterator is an iterable object. In short, the iterator is generated by _ _ iter__ () and can be called through _ _ next__ ().

In that case, when we learned the basics of Python, we said that range () is an iterable object, so it can also generate an iterator through _ _ iter__ ().

IV. Sequence

Sequence is also an abstract concept, which contains lists, tuples, and strings. It does not exist in itself, and it is also a concept word that is easy to learn.

Iterable objects contain sequences, and since sequences contain lists, tuples, and strings, which are also covered in our previous example, sequences can be used by iter () and next ().

Sequences can be divided into finite sequences and infinite sequences. A finite sequence has a range, for example, range (10) has a limited range, on the contrary, an infinite sequence is a sequence with no finite range.

We need to generate an infinite sequence, here we need to use a new module itertools,itertools for efficient loop iterative function set, under which there is a method count (), which can generate an iterator with no scope, which can be understood as an infinite iterator.

From this example, we can see that as long as it is executed once, next () will get the contents of the iterator and get them one by one. I have only written four next () here, and you will output more times if you write more times.

A mechanism like next (), which is called lazy loading mechanism whenever it is needed, is also called lazy loading.

On the contrary, there is hungry Chinese loading. For example, the for loop will get all the objects in the iterator as soon as it is executed.

Fifth, list derivation

List derivation is related to generators. Before talking about generators, you need to know what list deduction is. List derivation is a way to generate lists. The syntax is like this:

L = [i for i in iterable object]

I represents the object to be put in the list, and the for loop is a formula.

For example, let's use list derivation to generate a list:

L = [i for i in range (5)] print (l)

Running result:

[0, 1, 2, 3, 4]

Using list derivation, we can easily generate the list we want.

At the same time, it also has many flexible uses, such as adding conditional judgment after it.

L = [i for i in range (5) if 4

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report