python - 字典的元组列表

标签 python list dictionary tuples

这是我目前在 Python 中将元组列表转换为字典的方式:

l = [('a',1),('b',2)]
h = {}
[h.update({k:v}) for k,v in l]
> [None, None]
h
> {'a': 1, 'b': 2}

有没有更好的方法?似乎应该有一个单行来做到这一点。

最佳答案

只需调用 dict()直接在元组列表上

>>> my_list = [('a', 1), ('b', 2)]
>>> dict(my_list)
{'a': 1, 'b': 2}

关于python - 字典的元组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6522446/

相关文章:

python - bool 类型列作为 SQLAlchemy 中的 ClauseElement

python - Flask + MySQL 奇怪的行为

python - 磁盘上有 70 MB 但内存中有 500 MB 的列表

php - 使用 python 或 php 处理音频文件

python - 优化 : Amazon Redshift funcitons to check if daylight savings is in effect

android - 如何将对象列表转换为 jsonObject?

java - 将数组中的每个元素添加到列表中

javascript - 在 CouchDB Map 函数中发出日期时 - 日期对象上调用了什么?

c# - 为什么 Dictionary.Add 会覆盖我字典中的所有项目?

python - 有人可以解释这是如何通过字典循环的吗?