简体   繁体   English

如何在Python中将列表列表转换为列表字典?

[英]How can I convert a list of lists into a dictionary of lists in Python?

I have a list with four lists in it. 我有一个清单,里面有四个清单。 I want to assign each inner list a name, basically convert the list with four lists into a dictionary with four named lists. 我想为每个内部列表分配一个名称,基本上将具有四个列表的列表转换为具有四个命名列表的字典。 How can I do that in a Pythonic way? 我该如何用Python的方式做到这一点? I saw this question but it was too specfic. 我看到了这个问题,但这太具体了。

dictionary["list1"] = lists[0]
dictionary["list2"] = lists[1]
dictionary["list3"] = lists[2]
dictionary["list4"] = lists[3]

Edit: I would like to know how to do that with arbitrary dictionary keys - not just list1,list2,list3, etc. 编辑:我想知道如何使用任意字典键-不只是list1,list2,list3等。

This should do the trick 这应该可以解决问题

import itertools

names = "mains_voltage mains_time CT_voltage CT_time".split()
answer = {name:L for name,L in itertools.izip(names, lists)}

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

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