python 中的map,imap,startmap
来源:互联网 发布:excel的数据库管理功能 编辑:程序博客网 时间:2024/06/09 23:28
先来看看map
其定义为map(function,iterable1,iterable2, ……….)
针对iterable中的每个元素执行function函数,返回function执行结果的一个list列表,如果iterable不止一个,那么并行的选取每个iterable中元素,作为function的参数,然后执行function,下面看看列子:
a=[1,2,3]fun1=lambda a:a**asum=map(fun1,a)print(type(sum))print(sum)打印的结果:<type 'list'>[1, 4, 27]
其中iterable必须是可迭代对象,像list,set,dict都可以。
下面是有多个iterable的例子:
a=[1,2,3]b=[1,2,3]c=[1,2,3]#a=map(iter,a,b,c)fun=lambda a,b,c:a+b+csum=map(fun,a,b,c)print(type(sum))print(sum)fun1=lambda a:aprint(a)打印结果:<type 'list'>[3, 6, 9]
从结果可看出,针对每个iterable中相同位置的元素执行function ,[1+1+1 ,2+2+2 ,3+3+3]=[3,6,9]。
有一种特殊情况,如果map中function为None:
a=[1,2,3]b=[1,2,3]c=[1,2,3]#a=map(iter,a,b,c)fun=lambda a,b,c:a+b+csum=map(None,a,b,c)print(type(sum))print(sum)打印结果:<type 'list'>[(1, 1, 1), (2, 2, 2), (3, 3, 3)]
从打印结果可以看出,map返回的同样是列表,但是列表中每个元素是所有iterable中同位置对应的元素组成的元祖。
再来看看imap
imap的重点是yield,不明白的可以去我的上一篇blog查查
def imap(function, *iterables): # imap(pow, (2,3,10), (5,2,3)) --> 32 9 1000 iterables = map(iter, iterables) while True: args = [next(it) for it in iterables] if function is None: yield tuple(args) else: yield function(*args)
a=[1,2,3]b=[1,2,3]c=[1,2,3]fun=lambda a,b,c:a+b+cdef imapp(fun, *iterables): a = map(iter, iterables) while True: args = [next(it) for it in a] if fun is None: yield tuple(args) else: yield fun(*args)print(imapp(fun, a, b, c))for item in imapp(fun,a,b,c): print(item)打印结果:<generator object imapp at 0x0000000003C7CEA0>369
从结果看出和map基本相似,imap中同样使用了map。imapp反回了一个生成器generator。生成器函数在Python中与迭代器协议的概念联系在一起。简而言之,包含yield语句的函数会被特地编译成生成器。当函数被调用时,他们返回一个生成器对象,这个对象支持迭代器接口。函数也许会有个return语句,但它的作用是用来yield产生值的。
不像一般的函数会生成值后退出,生成器函数在生成值后会自动挂起并暂停他们的执行和状态,他的本地变量将保存状态信息,这些信息在函数恢复时将再度有效。
其中a=map(iter,iterables)中返回一个list,list中元素为listiterator object。然后后面的for循环针对每个object执行next(object)获取元素。
再来说说starmap
def starmap(function, iterable): # starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000 for args in iterable: yield function(*args)
从其实现可以看出,istartmap只有一个iterable,其中function的参数已经组织在了元祖中,而不是和map似的,需要运行时再guoup在一起。
- python 中的map,imap,startmap
- 字典,python中的map
- Python 中的 map() reduce()
- python中的map
- python中的map()函数
- Python中的map函数
- python中的map函数
- python中的map()函数
- python中的map函数
- 深入一步探索Python计算身份证校验位的代码写法【map -> imap】
- 关于Python 中的 map()函数
- python 3中的map使用
- Python中的函数特性map(),
- python中的map,reduce,filter
- imap send email by python
- python取得IMAP服务器邮件
- IMAP
- IMAP
- js点击关闭当前浏览器Scripts may close only the windows that were opened by it.
- Python 箴言
- Java加密算法---MD5
- 例题 2.8 快速排序
- could not start running tests with command 'pybot.bat'
- python 中的map,imap,startmap
- <sdut-ACM>顺序表应用2:多余元素删除之建表算法
- Python的ORM框架SQLAlchemy使用入门(二)【连接MySql数据库】
- 多线程编程-互斥锁
- 04:字符串分割 substr()
- Android PhotoView :一款扩展自ImageView 支持通过单点/多点触摸来进行图片缩放的智能控件。
- 常见的Hash算法
- jQuery中json对象与json字符串互换
- 打乱数组