我有一个这样的二元组列表:
l = [('a', 1), ('b', 2)]
我希望能够将其映射到一个字典对象上,这样我就可以做类似的事情
l.a #=> 1
所以我试过了,但为什么失败了?
d = reduce(lambda y,x : y.update({x[0]:x[1]}),l,{})
这给出了错误:
AttributeError: 'NoneType' object has no attribute 'update'
我做错了什么?
最佳答案
>>> l = [('a', 1), ('b', 2)]
>>> d = dict(l)
>>> d['a']
1
关于python - 从 2 元组列表创建字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6072470/