问题
I wrote some code that generates three separate lists that revolve around projects. The first list is a list of IDs, the second list is a list start dates and third list is a list of end dates. I would like to combine these lists into a single nested dictionary.
item[0]
from start_date
and item[0]
from end_date are associated with item[0]
from project_id[0]
project_id = ['project 1','project 2', 'project 3', 'project 4']
start_date = [datetime(2015,1,12), datetime(2015,1,13), datetime(2015,1,11), datetime(2015,1,13)]
end_date = [datetime(2015,1,15), datetime(2015,1,17), datetime(2015,1,15), datetime(2015,1,14)]
Desired Output:
d = {
'project 1' : {'start date' : datetime(2015,1,12), 'end date' : datetime(2015,1,15},
'project 2' : {'start date' : datetime(2015,1,13), 'end date' : datetime(2015,1,17},
'project 3' : {'start date' : datetime(2015,1,11), 'end date' : datetime(2015,1,15},
'project 4' : {'start date' : datetime(2015,1,13), 'end date' : datetime(2015,1,14},
}
回答1:
Use a dict comprehension and zip:
d = {a:{"start date":b,"end date":c } for a,b,c in zip(project_id,start_date,end_date)}
print d
{'project 4': {'end date': datetime.datetime(2015, 1, 14, 0, 0), 'start date': datetime.datetime(2015, 1, 13, 0, 0)}, 'project 2': {'end date': datetime.datetime(2015, 1, 17, 0, 0), 'start date': datetime.datetime(2015, 1, 13, 0, 0)}, 'project 3': {'end date': datetime.datetime(2015, 1, 15, 0, 0), 'start date': datetime.datetime(2015, 1, 11, 0, 0)}, 'project 1': {'end date': datetime.datetime(2015, 1, 15, 0, 0), 'start date': datetime.datetime(2015, 1, 12, 0, 0)}}
If you want a completely ordered dict use one:
from collections import OrderedDict
d = OrderedDict()
for a,b,c in zip(project_id,start_date,end_date):
d.setdefault(a,OrderedDict())
d[a]["start date"] = b
d[a]["end date"] = c
print(d)
{'project 1': OrderedDict([('start date', datetime.datetime(2015, 1, 12, 0, 0)), ('end date', datetime.datetime(2015, 1, 15, 0, 0))]),
'project 2': OrderedDict([('start date', datetime.datetime(2015, 1, 13, 0, 0)), ('end date', datetime.datetime(2015, 1, 17, 0, 0))]),
'project 3': OrderedDict([('start date', datetime.datetime(2015, 1, 11, 0, 0)), ('end date', datetime.datetime(2015, 1, 15, 0, 0))]),
'project 4': OrderedDict([('start date', datetime.datetime(2015, 1, 13, 0, 0)), ('end date', datetime.datetime(2015, 1, 14, 0, 0))])}
回答2:
from datetime import datetime
if __name__=='__main__':
project_id = ['project 1','project 2', 'project 3', 'project 4']
start_date = [datetime(2015,1,12), datetime(2015,1,13), datetime(2015,1,11), datetime(2015,1,13)]
end_date = [datetime(2015,1,15), datetime(2015,1,17), datetime(2015,1,15), datetime(2015,1,14)]
d = dict()
for idx in range(0,len(project_id)):
d[project_id[idx]] = {'start date': start_date[idx], 'end date': end_date[idx]}
print d
回答3:
d = {}
for project, start, end in zip(project_id, start_date, end_date):
d[project] = {'start date': start, 'end date': end}
Assuming that all of your information lines up in the lists, this will generate your desired output.
回答4:
Basically, we're iterating through the indices of the project list. Using these indices, we create a sub-dictionaries grabbing the elements in the other lists of the same index. Then, we pair this value with the project id key.
d = {}
for i in range(len(project_id)):
sub = {'start date': start_date[i], 'end date': end_date[i]}
d[project_id[i]] = sub
回答5:
from datetime import datetime
d = {a:{"start date":b.strftime("%Y, %m, %d"),"end date":c.strftime("%Y, %m, %d") } for a,b,c in zip(project_id,start_date,end_date)}
print(d)
result
{'project 4': {'end date': '2015, 01, 14', 'start date': '2015, 01, 13'}, 'project 2': {'end date': '2015, 01, 17', 'start date': '2015, 01, 13'}, 'project 3': {'end date': '2015, 01, 15', 'start date': '2015, 01, 11'}, 'project 1': {'end date': '2015, 01, 15', 'start date': '2015, 01, 12'}}
来源:https://stackoverflow.com/questions/28100170/created-a-nested-dictionary-from-three-separate-lists