从三个单独的列表创建嵌套字典

时间:2015-01-22 22:55:52

标签: python python-2.7 dictionary dictionary-comprehension

我写了一些代码,生成了三个围绕项目的单独列表。第一个列表是ID列表,第二个列表是列表开始日期,第三个列表是结束日期列表。我想将这些列表组合成一个嵌套字典。

来自item[0]

start_date和来自end_date的item[0]与来自item[0]

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

期望的输出:

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

5 个答案:

答案 0 :(得分:2)

使用dict理解和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)}}

如果你想要一个完全有序的词典,请使用一个:

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

d = {}
for project, start, end in zip(project_id, start_date, end_date):
    d[project] = {'start date': start, 'end date': end}

假设您的所有信息都排在列表中,这将生成您想要的输出。

答案 3 :(得分:1)

基本上,我们会遍历项目列表的索引。使用这些索引,我们创建了一个子字典,用于获取同一索引的其他列表中的元素。然后,我们将此值与项目ID键配对。

d = {}

for i in range(len(project_id)):
    sub = {'start date': start_date[i], 'end date': end_date[i]}
    d[project_id[i]] = sub

答案 4 :(得分:-1)

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)

结果

{'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'}}