我有一个结构如下:
{ 'records':[['15','2013-04-02','Mexico','blah','bleh',1,2],['25','2013-04-02','Italy','meh','heh',3,4]], 'attributes':['id','date','location','descr1','descr2','total1','total2'] }
Run Code Online (Sandbox Code Playgroud)
它是使用json.load从json创建的.
如何迭代记录键以使['records'] [0]成为新词典中的键,['records']中每个列表的其余部分为该键的值.
像这样的东西是我在想的,甚至可能都不可能,我是Python的新手:
{ '15':['2013-04-02','Mexico','blah','bleh',1,2], '25':['2013-04-02','Italy','meh','heh',3,4] }
Run Code Online (Sandbox Code Playgroud)
有人能指出我正确的方向来迭代原始的dict来创建新的dict吗?
如果d是你的字典:
In [5]: {rec[0]:rec[1:] for rec in d['records']}
Out[5]:
{'15': ['2013-04-02', 'Mexico', 'blah', 'bleh', 1, 2],
'25': ['2013-04-02', 'Italy', 'meh', 'heh', 3, 4]}
Run Code Online (Sandbox Code Playgroud)