繁体   English   中英

如何将元组列表转换为以索引为键的字典

[英]How to convert list of tuples to dictionary with index as key

我正在尝试将元组列表转换为以列表索引为键的字典。

m = [(1, 'Sports', 222), 
     (2, 'Tools', 11),
     (3, 'Clothing', 23)]

到目前为止,我已经尝试使用:

dict((i:{a,b,c}) for a,b,c in enumerate(m))

但这不起作用。

我的预期输出是:

{0: [1, 'Sports', 222],
 1: [2, 'Tools', 11],
 2: [3, 'Clothing', 23]}

使用以下词典理解:

>>> {i:list(t) for i, t in enumerate(m)}
{0: [1, 'Sports', 222], 1: [2, 'Tools', 11], 2: [3, 'Clothing', 23]}

它会工作

tuple_list = [(1, 'Sports', 222), (2, 'Tools', 11), (3, 'Clothing', 23)]

output_dict = {}
for index, data in enumerate(tuple_list):
    output_dict[index] = list(data)

print(output_dict)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM