简体   繁体   中英

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? 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.

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)}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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