def f(x,y):
return x*10+y
def g(s):
return{'0':0,'1':1,'2':2,'3':3,'4':4,'5':5}[s]
reduce(f,map(g,'1235'))
1235
若单独运行g(s)函数:
g('1235')
KeyError raceback (most recent call last)
in ()
----> 1 g('1235')
in g(s)
2 return x*10+y
3 def g(s):
----> 4 return{'0':0,'1':1,'2':2,'3':3,'4':4,'5':5}[s]
5
KeyError: '1235'
问题:为什么函数单独拿出来运行会出错?