python - 从 2 元组列表创建字典

标签 python dictionary reduce

我有一个这样的二元组列表:

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/

相关文章:

python - 如何在一次传递的 Spark 中添加元组列表中第二个元素的长度?

python - 如何调试 ImportError(sys.path 正确)

python - 查找 numpy 数组中超过先前值的值的索引

c++ - 删除标准库映射

c# - 调用 System.Drawing.Image.Save 时出现无效参数错误

javascript - 用不同标签的对象中的数组汇总数组

python - Azure 上的 Django + FastCGI 应用程序,频繁重启

python - psycopg2 - Postgresql 数据库中的 'Relation does not exist'

c++ - 清除嵌套数据

Python 字典 : why and when I have to use sometimes ":" and sometimes "=" between the key and the value?