Iterator of Python and Detailed Explanation of zip

  • 2021-12-12 04:49:19
  • OfStack

Catalog on Iterator on zip Summary:

First of all, throw out a question that has puzzled me for a long time:


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

What about the console output?

On Iterators

1. For collection objects that support iteration, 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. All the elements are accessed. The iterator is consumed (still occupying the address), but the stored information (i.e. address and traversal location) is emptied. Use the list () function or the * operator to get all the contents of the traversal, after which the iterator is consumed.


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

The output is:

< list_iterator object at 0x00000205944619D0 >
[1,2,3,4,5,6]
< list_iterator object at 0x00000205944619D0 >
[]

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

2. Copy the iterator object and get a reference to the iterator object, that is, the address, instead of creating a new iterator. Therefore, traversal locations are common.


nums = [1,2,3,4,5,6]
numsIter = iter(nums)
numsIter_list = [numsIter] * 2
print(numsIter)
print(numsIter_list)

for _ in numsIter_list:
    print(next(_))

The output is:

< list_iterator object at 0x0000020594445A00 >
[ < list_iterator object at 0x0000020594445A00 > , < list_iterator object at 0x0000020594445A00 > ]
1
2

About zip

1. When creating an zip object, new data is not generated directly, but an iterator to manipulate the object is stored, and the data is traversed through the iterator when it is really needed. Thus, when an zip object is decompressed using the list () function or the * operator, of course, the stored iterator is consumed so that it returns to null on another decompression.


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

The output is:

< zip object at 0x0000020594520140 >
[(1, 4), (2, 5), (3, 6)]
[]

2. The reason why work can be work is that it uses the traversal position stored by iterators to obtain data one by one until one iterator is consumed. The question at the beginning of the article is the best example.


nums = [1,2,3,4,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 3 columns and 2 rows. How did this happen? When executing zip, three identical iterators are accessed in turn, and the iterator's traversal position will be added by 1 for each access, so zip obtains (1, 2, 3) after the end of the first round, and similarly obtains (4, 5, 6) in the second round, and then the iterator is consumed. The third round of zip object has no iterator available, so the execution ends. Finally print (list (numsIter) finds that the iterator has indeed been consumed.

3. When using print to display the contents of an iterator or an zip object, there is little difference between the list () and * operators. In the above example, however, it can only be implemented with the * operator, because the * operator actually returns the address and traversal position of the object, and list () will directly traverse and consume the iterator.


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

The output is:

< list_iterator object at 0x0000020594445B80 >
(1,)
(2,)
(3,)
(1,)
(2,)
(3,)
(1,)
(2,)
(3,)
[]

Summary:

Familiar with the iterator and zip () function at the same time, master a 1-dimensional data into 2-dimensional data method.

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: