posted on 2023-05-03 20:27 read(574) comment(0) like(13) collect(2)
**
**
(1) The map function is a built-in function in python for mapping.
(2) The map() function returns a new iterator object and will not change the original object!
class map(object) | map(func, *iterables) --> map object | | Make an iterator that computes the function using arguments from | each of the iterables. Stops when the shortest iterable is exhausted. | | Methods defined here: | | __getattribute__(self, name, /) | Return getattr(self, name). | | __iter__(self, /) | Implement iter(self). | | __next__(self, /) | Implement next(self). | | __reduce__(...) | Return state information for pickling. | | ---------------------------------------------------------------------- | Static methods defined here: | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature.
Simple understanding:
map(function, iterable)
#function -- 函数
#iterable -- 序列
In fact, the function of map() is very simple, you only need to remember one sentence: apply the function mapping to the sequence! !
(1) Use map() combined with split() for input
① Input str: a, b, c = input().split()
② Input integers: a, b, c = map(int, input().split( ))
③Input floating point numbers: a,b,c = map(float,input().split())
a,b,c = map(int,input().split(","))#此处function为int,序列即输入
print(a,b,c)
>>>1,2,3
>>>1 2 3
(2) map() returns an iterator
l = [2,0,0,2,0,2,2,2]
result = map(lambda x: x*x,l)
print(result)#返回一个迭代器
>>><map object at 0x000001DC51E22EC8>
print(list(result))#使用 list() 转换为列表
>>>[4, 0, 0, 4, 0, 4, 4, 4]
print(list(map(lambda x:x*x,l)))#合并为一行代码解决
>>>[4, 0, 0, 4, 0, 4, 4, 4]
Note: The map function will return an iterator, which is often converted using list().
Shallow records a small error (the code is as follows):
list = [2,0,0,2,0,2,2,2]
result = map(lambda x: x*x,list)
print(result)
print(list(result))
The result is an error:
TypeError: 'list' object is not callable
Demystification ( I never noticed this before! ):
I tried to convert the result returned by map() into a list list, but because the variable list and the function list have the same name , when the function uses the list function, it finds that list is a well-defined list, and the list cannot be called. Hence a TypeError is thrown.
(3) The function in the map is a custom function
def function(x):
return x*2
print(list(map(function,range(3))))
>>>[0, 2, 4]
(4) The iterator is accessed only once
l = ['1', '2', '3', '4', '5', '6']
print(l)
>>>['1', '2', '3', '4', '5', '6']
l_int = map(lambda x: int(x), l)
for i in l_int:
print(i, end=' ')
>>>1 2 3 4 5 6
print()
print(list(l_int))
>>>[]#由于map()生成的是迭代器,所以for循环以后再使用l_int时迭代器中的数据已经为空了!!!
Author:kimi
link:http://www.pythonblackhole.com/blog/article/292/bd27ab490dd11ce5a1dd/
source:python black hole net
Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.
name:
Comment content: (supports up to 255 characters)
Copyright © 2018-2021 python black hole network All Rights Reserved All rights reserved, and all rights reserved.京ICP备18063182号-7
For complaints and reports, and advertising cooperation, please contact vgs_info@163.com or QQ3083709327
Disclaimer: All articles on the website are uploaded by users and are only for readers' learning and communication use, and commercial use is prohibited. If the article involves pornography, reactionary, infringement and other illegal information, please report it to us and we will delete it immediately after verification!