是否可以一次将映射函数应用于多个值?
像这样的东西
from collections import defaultdict
d['a'] = [1,2,3,4]
d['b'] = [4,5,6,7]
d['a'], d['b'] = map(lambda x,y: (x,y) if x*y % 3 == 0 else (0,0), d['a'], d['b'])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-129-5191c9205e6f> in <module>()
----> 1 d['a'], d['b'] = map(lambda x,y: (x,y) if x*y % 3 == 0 else (0,0), d['a'], d['b'])
ValueError: too many values to unpack
当然也可以单独进行。
l = map(lambda x,y: x if x*y % 3 == 0 else 0, d['a'],d['b'])
m = map(lambda x,y: x if x*y % 3 == 0 else 0, d['b'],d['a'])
d['a'] = l
d['b'] = m
d
defaultdict(<type 'list'>, {'a': [0, 0, 3, 0], 'b': [0, 0, 6, 0]})