从2-Tuples列表创建字典 [英] Creating a Dictionary from a List of 2-Tuples
本文介绍了从2-Tuples列表创建字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的2元组列表:
I have a list of 2-tuples like this:
l = [('a', 1), ('b', 2)]
并且我希望能够将其映射到字典对象上,以便我可以做类似的事情
and I want to be able to map this onto a dictionary object, so that I can do something like
l.a #=> 1
所以我尝试了这个,但是为什么失败了?
So I tried this, but why does it fail?
d = reduce(lambda y,x : y.update({x[0]:x[1]}),l,{})
这给出了错误:
AttributeError:"NoneType"对象具有 没有属性更新"
AttributeError: 'NoneType' object has no attribute 'update'
我在做什么错了?
推荐答案
>>> l = [('a', 1), ('b', 2)]
>>> d = dict(l)
>>> d['a']
1
这篇关于从2-Tuples列表创建字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文