map的使用

 1   #一个函数f(x)=x2,把这个函数作用在一个list[1,2,3,4,5,6,7,8,9]
 2   # 传统的解法:
 3  a = [1,2,3,4,5,6,7,8,9]
 4   def f(x):
 5       return x*x
 6   result_list = []
 7   for i in a:
 8       result_list.append(f(i))
 9   print(result_list)
10  
11  #使用map实现
12  it = map(f,a)  # 返回的是一个可迭代的对象
13  print(type(it))
14  #判断是否是一个可迭代的对象
15  from collections import Iterable
16  print('判断是否是可迭代的:',isinstance(it,Iterable))
17  print(list(it))
1  [1, 4, 9, 16, 25, 36, 49, 64, 81]
2  <class 'map'>
3  d:My-python高阶函数高阶函数map的使用.py:15: DeprecationWarning: Using or importing the ABCs from 
4  'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working      
5    from collections import Iterable
6  判断是否是可迭代的: True
7  [1, 4, 9, 16, 25, 36, 49, 64, 81]
正是江南好风景
原文地址:https://www.cnblogs.com/monsterhy123/p/12895610.html