mapreduce shuffle作用(python中filter与map不同)
导读:本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。...
本文教程操作环境:windows7系统 、Python 3.9.1 ,DELL G3电脑 。
1.定义不同
map是Python的内置函数, 使用的方式如下:
list=map(func,iter)其中 , func是函数 , iter是可迭代的序列 。
它的功能是:将一个序列中的每一个元素应用传入的函数 , 并返回一个包含所有函数调用结果的一个列表
使用方式:
fromcollectionsimportIterable lst=[1,2,5,6,7] res=filter(lambdax:x%2==0,lst) #res是一个可迭代对象 print(isinstance(res,Iterable),type(res)) foriteminres: print(item)2.返回结果不同
函数名区别map作用于每个可迭代对象的元素 ,并返回处理之后的元素filter作用于可迭代内每个元素 ,根据计算后结果:True保留 ,Flase去掉
获取列表内所有的整数类型元素
defonly_int(x): try: ifisinstance(x,int): returnTrue else: returnFalse exceptValueErrorase: returnFalse dt=filter(type_int,[1,2,3,3,3232,-34.5,34.5]) >>>list(dt) [1,2,3,3]创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!