37,720
社区成员
发帖
与我相关
我的任务
分享
lt1 = [
{'goid':1111,'kid':222222,'cost':2,'date':'2012-02-11'},
{'goid':1112,'kid':222225,'cost':5,'date':'2012-05-11'},
{'goid':1111,'kid':222226,'cost':8,'date':'2012-02-11'},
]
lt2 = [
{
'1111': #以goid的值为key
{
'2012-02-11': #date的值为key
[
{'goid':1111,'kid':222222,'cost':2,'date':'2012-02-11'},
{'goid':1111,'kid':222226,'cost':8,'date':'2012-02-11'}
]
}
'1112':
{
'2012-05-11':
[
{'goid':1112,'kid':222225,'cost':5,'date':'2012-05-11'}
]
}
}
]
>>> lt1 = [ {'goid':1111,'kid':222222,'cost':2,'date':'2012-02-11'}, {'goid':1112,'kid':222225,'cost':5,'date':'2012-05-11'}, {'goid':1111,'kid':222226,'cost':8,'date':'2012-02-11'}, ]
>>> def transform(lt):
... result = {}
... for d in lt:
... result.setdefault(d['goid'], {}).setdefault(d['date'], []).append(d)
... return [result]
>>> transform(lt1)
9: [{1111: {'2012-02-11': [{'cost': 2,
'date': '2012-02-11',
'goid': 1111,
'kid': 222222},
{'cost': 8,
'date': '2012-02-11',
'goid': 1111,
'kid': 222226}]},
1112: {'2012-05-11': [{'cost': 5,
'date': '2012-05-11',
'goid': 1112,
'kid': 222225}]}}]
>>>