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

How to understand the iterator and zip of Python

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

Share

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

This article mainly explains "how to understand the iterator and zip of Python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the iterator and zip of Python".

First of all, I would like to raise a question that has plagued me for a long time:

Nums = [1 numsIter 2 3 4 5 5 6] numsIter = iter (nums) for _ in zip (* [numsIter] * 3): print (_) print (list (numsIter))

What about the console output?

About iterators

1. For collection objects that support iterations, you can create their iterator objects. The iterator object stores the address and traversal location of the iterable object, the iterator object is accessed from the first element of the collection, and all elements are accessed and the iterator is consumed (still occupying the address). But clear the stored information (that is, address and traversal location). You can get all the contents of the traversal using the list () function or the * operator, and the iterator is consumed when it is over.

Nums = [1, nums, 3, 4, 5, 6] numsIter = iter (nums) print (nums) print (list (nums)) print (nums) print (list (nums))

The output is:

[1,2,3,4,5,6]

[]

The second time you type print (list (nums)), the output is empty because the iterator object can no longer find the address.

two。 Copy an iterator object and get a reference to an iterator object, the address, instead of creating a new iterator. Therefore, traversing locations is common.

Nums = [1 in numsIter_list 2 3 in numsIter_list 4 5 6] numsIter = iter (nums) numsIter_list = [numsIter] * 2print (numsIter) print (numsIter_list) for _ in numsIter_list: print (next (_))

The output is:

[,]

one

two

About zip

1. When you create a zip object, instead of generating new data directly, you store an iterator to manipulate the object, traversing the data through the iterator when you really need it. Therefore, when you use the list () function or the * operator to extract the zip object, of course, it consumes the stored iterator, so that it returns empty when unzipped again.

P = [1Jing 2 Jing 3] Q = [4 Jing 5 Jing 6] PQ_zip = zip (PMagne Q) print (PQ_zip) print (list (PQ_zip)) print (list (PQ_zip))

The output is:

[(1,4), (2,5), (3,6)]

[]

The reason why 2.zip is able to work is that it takes advantage of the traversal location stored by the iterator to get the data one by one until one of the iterators is consumed. The question of the beginning of the article is the best example.

Nums = [1 numsIter 2 3 4 5 5 6] numsIter = iter (nums) for _ in zip (* [numsIter] * 3): print (_) print (list (numsIter))

The output is:

(1, 2, 3)

(4, 5, 6)

[]

As you can see, the function is to change a single row of data into three columns and two rows. How do you do that? When you execute zip, you access three same iterators in turn, and the traversal position of the iterator is incremented by 1 for each visit, so the zip gets it at the end of the first round (1mem2, 3), and in the same way, the second round gets it (4, 5, 5, 6), then the iterator is consumed. The third round of zip objects has no iterators available, so execution ends. Finally, print (list (numsIter)) found that the iterator had indeed been consumed.

3. When you use print to display the contents of an iterator or zip object, there is little difference between the list () and * operators. But in the above example, you can only use the * operator, because the * operator actually returns the address and traversal position of the object, while list () traverses directly, consuming the iterator.

Nums = [1Jing 2 Jue 3] numsIter = iter (nums) print (numsIter) for _ in zip (list (numsIter) * 3): print (_) print (list (numsIter))

The output is:

(1,)

(2,)

(3,)

(1,)

(2,)

(3,)

(1,)

(2,)

(3,)

[]

Thank you for your reading, the above is the content of "how to understand Python iterator and zip". After the study of this article, I believe you have a deeper understanding of how to understand Python iterator and zip, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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