为什么将 map 函数的结果分配给一个变量与将其分配给多个变量不同?

Why does assigning result of a map function to one variable differs from assigning it to several variables?

这里我输入了 2 个字符串 "12""123 54 856 78 " 到同一个函数,但一个被转换为 int 而另一个没有:

str="12"
a=map(int,str.split())
print(type(a)) #o/p :<class 'map'>
print (a) #o/p :<map object at 0x7f432e95a630>


str1="123 54 856 78 "
a1,b1,c1,d1=map(int,str1.split())
print(type(a1)) #o/p :<class 'int'>
print (a1) #o/p :123

在第一次调用地图 (a=map(int,str.split())) 时,您将 a 设置为地图对象。在第二次调用 map (a1,b1,c1,d1=map(int,str1.split())) 时,您将 map 函数的值设置为 map 函数生成的不同值。如果你有

a1 = map(int(str1.split())
print(type(a1)) # <class 'map'>

地图函数:

Python map() function is used to apply a function on all the elements of specified iterable and return map object. Python map object is an iterator, so we can iterate over its elements. We can also convert map object to sequence objects such as list, tuple etc. using their factory functions.

We can pass multiple iterable arguments to map() function, in that case, the specified function must have that many arguments. The function will be applied to these iterable elements in parallel. With multiple iterable arguments, the map iterator stops when the shortest iterable is exhausted.

来源:https://www.journaldev.com/22960/python-map-function

这意味着默认情况下 map 函数 returns 一张地图。但是当指定 more 作为一个变量时,它会尝试将地图中的项目映射到指定的变量。所以在字符串中只使用一个变量和一个值是行不通的。

这就是地图功能的工作原理。