将元组转换为字典中的列表 [英] Convert tuple to list in a dictionary
本文介绍了将元组转换为字典中的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一本这样的字典:
a= {1982: [(1,2,3,4)],
1542: [(4,5,6,7),
(4,6,5,7)]}
,我想将所有元组(1,2,3,4),(4,5,6,7),(4,6,5,7)
更改为列表,在这种情况下:[1,2,3,4], [4,5,6,7], [4,6,5,7]
and I want to change the all the tuples (1,2,3,4),(4,5,6,7),(4,6,5,7)
to lists, in this case: [1,2,3,4], [4,5,6,7], [4,6,5,7]
我尝试过
for key, value in a.items():
for i in value:
i = tuple(i)
但是它现在可以工作了.我该如何实现?
but it does now work. How can I achieve it?
推荐答案
据我了解,您希望转换列表中的每个元组.您可以使用字典理解来做到这一点:
As far as I understand you want to convert each tuple in a list. You can do this using a dictionary comprehension:
{k: [list(ti) for ti in v] for k, v in a.items()}
会给予
{1542: [[4, 5, 6, 7], [4, 6, 5, 7]], 1982: [[1, 2, 3, 4]]}
那是你的追随吗?
这篇关于将元组转换为字典中的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文