[英]Convert lists of list into List of dictionaries
I want to convert this list of lists to list of dictionaries as the following format using python3.我想使用 python3 将此列表列表转换为以下格式的字典列表。
[['a','b','c'],['d','e'],['f','g']]
The value to key need to be 1 for all list elements.对于所有列表元素,键的值必须为 1。 Any guide please.
请提供任何指导。
[{'a':1,'b':1,'c':1},{'d':1,'e':1},{'f':1,'g':1}]
You can do this in a single line using a combination of list and dictionary comprehension.您可以使用列表和字典理解的组合在一行中完成此操作。 Assume
a
is your original list:假设
a
是您的原始列表:
a = [['a','b','c'],['d','e'],['f','g']]
The you would get your desired output with:你会得到你想要的 output :
b = [{k: 1 for k in i} for i in a]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.