Python - Talking about the iterator object returned by map in Python3

what is iterator

  • An iterator is an object that remembers where to traverse.
  • Iterator objects are accessed from the first element of the collection until all elements have been accessed.
  • Iterators can only go forward and not backward.
  • Two basic methods iter()andnext()
ls = [1, 2, 3]
it = iter(ls)    # 创建迭代器对象
print(next(it))  # 输出迭代器的下一个元素  1
print(next(it))  # 2
print(next(it))  # 3
print(next(it))  # 直至结束,报错提示:StopIteration

What is an iterable object

  • __iter__Check whether the object is an iterable object by checking whether there is a special method in the method of the object .
  • dir()You can check all the methods of the object through the function; or hasattr()check whether there is a special method.
  • What are the common iterable objects: str list tuple dic set etc.
ls = [1, 2, 3]
print(hasattr(ls, '__iter__'))     # True

map function

  • map() will map the specified iterable object according to the provided function.
  • grammar:map(function, iterable, ...)
  • Return value: python2 returns a list; python3 returns an iterator (this article discusses python3)
ls = [1, 2, 3]
it = map(lambda x: x+1, ls)
print(it)   # 返回map对象地址,<map object at 0x0000024D4A0D39A0>
print(hasattr(it, '__iter__'))   # True,是个可迭代对象

experiment

Let's write it differently, without anonymous functions.

def func(x):
	return x + 1
	
ls = [1, 2, 3]
it = map(func, ls)
print(it)
print(next(it))   # 2  因为有返回值,next(it)返回2
print(next(it))   # 3
print(next(it))   # 4

Let's look at a situation:

def func(x):
	return x + 1
	
ls = [1, 2, 3]
it = map(func, ls)
print(it)
print(next(it))   # 2  因为有返回值,next(it)返回2
print(list(it))   # [3, 4] 因为1已经遍历过了,从2开始遍历,剩下的元素被转换成列表

Usually we use map to convert the map object into a list or other iterable objects.
As can be seen from the previous piece of code, during the conversion process, it is actually equivalent to next traversing the object and converting the returned result into a list.


The above are all functions with return values. If there is no return value, it is impossible to get the value through next(), it is just a processing action.
Simplified version of the problem encountered :
For example, I want to use map to batch process the two-dimensional list, that is, change the 0th element of each second-level list to 7, and there is no return value here.

def fu(a):
    a[0] = 7

ls = [[1, 2], [2, 3], [5, 6]]
x = map(fu, ls)
print(next(x))  # None
print(ls)       # 查看当前的ls列表,[[7, 2], [2, 3], [5, 6]]
next(x)
print(ls)       # 查看当前的ls列表,[[7, 2], [7, 3], [5, 6]]
next(x)
print(ls)       # 查看当前的ls列表,[[7, 2], [7, 3], [7, 6]]

It can be seen that if there is no next, no operation will be performed. Therefore, map just creates an iterator object, and does not process it after the map is completed.

def fu(a):
    a[0] = 7

ls = [[1, 2], [2, 3], [5, 6]]
x = map(fu, ls)
print(ls)       # [[1, 2], [2, 3], [5, 6]]  应证只创建了迭代器对象
list(x)
print(ls)       # [[7, 2], [7, 3], [7, 6]]

We list the iterator and get the final result. Therefore, the list traverses all the iterators.
Print list(x), the result is [None, None, None], it turned out to be just a tool to help us operate ls.
Because in the problem encountered, if I don't convert or traverse the iterable object to the map, ls will never change.

* It’s just a brief talk, so it’s just an analysis and summary from the experimental phenomenon. If there is something wrong, please correct me!

Guess you like

Origin blog.csdn.net/DreamingBetter/article/details/125002705