nem*_*ixx 6 python reduce dictionary
我有一个像这样的2元组列表:
l = [('a', 1), ('b', 2)]
Run Code Online (Sandbox Code Playgroud)
我希望能够将它映射到字典对象上,以便我可以做类似的事情
l.a #=> 1
Run Code Online (Sandbox Code Playgroud)
所以我尝试了这个,但为什么会失败?
d = reduce(lambda y,x : y.update({x[0]:x[1]}),l,{})
Run Code Online (Sandbox Code Playgroud)
这给出了错误:
AttributeError:'NoneType'对象没有属性'update'
我究竟做错了什么?
And*_*oev 21
>>> l = [('a', 1), ('b', 2)]
>>> d = dict(l)
>>> d['a']
1
Run Code Online (Sandbox Code Playgroud)