字典的元组列表 [英] List of tuples to dictionary
本文介绍了字典的元组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我目前在 Python 中将元组列表转换为字典的方法:
l = [('a',1),('b',2)]h = {}[h.update({k:v}) for k,v in l]>[无,无]H>{'a':1,'b':2}
有没有更好的办法?似乎应该有一个单线来做到这一点.
解决方案
只需调用 dict()
直接上元组列表
Here's how I'm currently converting a list of tuples to dictionary in Python:
l = [('a',1),('b',2)]
h = {}
[h.update({k:v}) for k,v in l]
> [None, None]
h
> {'a': 1, 'b': 2}
Is there a better way? It seems like there should be a one-liner to do this.
解决方案
Just call dict()
on the list of tuples directly
>>> my_list = [('a', 1), ('b', 2)]
>>> dict(my_list)
{'a': 1, 'b': 2}
这篇关于字典的元组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文