将具有公共密钥的项添加到python中的字典

时间:2015-07-11 15:09:22

标签: python dictionary

我有这个:

items = {{'project':'Project 1','description':'Task description','time':1222222},
{'project':'Project 2','description':'Task description 2','time':1224322},
{'project':'Project 1','description':'Task description 3','time':13222152}}

我需要这样的事情:

resultitems = {
    'project':'Project 1','pritems':{
        {'description':'Task description','time':1222222},
        {'description':'Task description 3','time':13222152}},
    'project':'Project 2',pritems':{
        {'description':'Task description 2','time':1224322}},
    } 

简单地将每个项目的名称作为关键

我尝试过这种方法:

resultitems = {}
resultitems['Project 2'] = {}
resultitems['Project 2'].update(..)

更新不起作用,因为它取代了以前的值

在PHP中,它很容易,

$resultitems['Project 2'][] = array(...)

但是没有找到在Python中执行此操作的方法

3 个答案:

答案 0 :(得分:0)

result_items = {
    'house project': [{'task': 'cleaning', 'hours': 20}, {'task': 'painting', 'hours: 30', etc.],
    'garden project': [{'task': 'mowing the lawn', 'hours': 1, etc.
    etc.
}

答案 1 :(得分:0)

您的变量'项目'是不正确的。如果它是一个词典列表,它应该是:

items = [{...},{...},{...}]

请写下数据来源,从哪里获取数据。这将决定您填写所需词典的方式。如果您已经拥有'项目中的数据。 (即字典列表),然后是如何转换它:

items = [{'project':'Project 1','description':'Task description','time':1222222},
{'project':'Project 2','description':'Task description 2','time':1224322},
{'project':'Project 1','description':'Task description 3','time':13222152}]

dct = {}
for e in items :
    if e['project'] not in dct :
        dct[e['project']] = []
    dct[e['project']].append(dict([(k, v) for k,v in e.items() if k != 'project']))
print dct

,输出为:

{'Project 2': [{'description': 'Task description 2', 'time': 1224322}], 'Project 1': [{'description': 'Task description', 'time': 1222222}, {'description': 'Task description 3', 'time': 13222152}]}

答案 2 :(得分:0)

最后,我使用了这个:

newdata = {}

for data in result['data']:
    try:
        newdata[data['project']].append({"description":data['description'],"start":data['start'],"time":data['dur']})
    except:
        newdata[data['project']] = []
        newdata[data['project']].append({"description":data['description'],"start":data['start'],"time":data['dur']})

print newdata

结果是这样的,这就是我所需要的:

{
u'Project 1': [
    {'start': u'2015-07-09T18:09:41-03:00', 'description': u'Task 1 name', 'time': 1432000}, 
    {'start': u'2015-07-09T17:42:36-03:00', 'description': u'Task 2 name', 'time': 618000}
    ], 
u'Project 2': [
    {'start': u'2015-07-09T20:14:16-03:00', 'description': u'Other Task Name', 'time': 4424000}
    ], 
u'Project 3': [
    {'start': u'2015-07-09T22:29:51-03:00', 'description': u'another task name for pr3', 'time': 3697000}, 
    {'start': u'2015-07-09T19:38:02-03:00', 'description': u'something more to do', 'time': 59000}, 
    {'start': u'2015-07-09T19:11:49-03:00', 'description': u'Base tests', 'time': 0}, 
    {'start': u'2015-07-09T19:11:29-03:00', 'description': u'Domain', 'time': 0}
], 
u'Project something': [
    {'start': u'2015-07-09T19:39:30-03:00', 'description': u'Study more', 'time': 2069000}, 
    {'start': u'2015-07-09T15:46:39-03:00', 'description': u'Study more (2)', 'time': 3800000}, 
    {'start': u'2015-07-09T11:46:00-03:00', 'description': u'check forms', 'time': 660000}
]
}
顺便说一句,我并不是在询问结构本身......相反,我需要的是编程一个像这样的东西"结构