python map和reduce的用法

来源:互联网 发布:部落冲突石头升级数据 编辑:程序博客网 时间:2024/03/29 18:56

 

>>> L1 = ['a', 'b', 'c']>>> L2 = [1, 2, 3]>>> zip(L1, L2)[('a', 1), ('b', 2), ('c', 3)]>>> map(L1, L2)Traceback (most recent call last):  File "", line 1, in ?TypeError: 'list' object is not callable>>> map(None, L1, L2)[('a', 1), ('b', 2), ('c', 3)]>>> dict(zip(L1, L2)){'a': 1, 'c': 3, 'b': 2}>>> dict(map(None, L1, L2)){'a': 1, 'c': 3, 'b': 2}>>> L3 = []>>> zip(L1, L3)[]>>> map(L1, L3)[]>>> map(None, L1, L3)[('a', None), ('b', None), ('c', None)]>>> dict(zip(L1, L3)){}>>> dict(map(None, L1, L3)){'a': None, 'c': None, 'b': None}>>> def fun(A, B):...     if B == None: B = ''...     return A, B...map(fun, L1, L2)[('a', 1), ('b', 2), ('c', 3)>>> map(fun, L1, L3)[('a', ''), ('b', ''), ('c', '')]>>> dict(map(fun, L1, L3)){'a': '', 'c': '', 'b': ''}

原创粉丝点击