重组词典
Reorganize Dictionary
我有一个 .csv 文件,我使用 csv.dictwriter 将其读入 python,看起来像这样:
[
{'id':'1', 'date':'12312004','var1':'234', 'var2':'456'},
{'id':'1', 'date':'12312005','var1':'200', 'var2':'466'},
{'id':'1', 'date':'12312006','var1':'760', 'var2':'656'},
{'id':'1', 'date':'12312007','var1':'215', 'var2':'256'},
{'id':'2', 'date':'5312006','var1':'234', 'var2':'456'},
{'id':'2', 'date':'5312007','var1':'200', 'var2':'466'},
{'id':'2', 'date':'5312008','var1':'760', 'var2':'656'},
{'id':'2', 'date':'5312009','var1':'215', 'var2':'256'},
]
我想将其重组为:
{'1': {
'12312004': {'var1': '234', 'var2': '456'},
'12312005': {'var1': '200', 'var2': '466'},
'12312006': {'var1': '760', 'var2': '656'},
'12312007': {'var1': '215', 'var2': '256'}},
'2': {
'5312006': {'var1': '234', 'var2': '456'},
'5312007': {'var1': '200', 'var2': '466'},
'5312008': {'var1': '760', 'var2': '656'},
'5312009': {'var1': '215', 'var2': '256'}}
}
如果您输入的是一个列表,您可以这样做:
output = {}
for entry in input_list:
entry = entry.copy() # so we can mangle it
id = entry.pop('id')
date = entry.pop('date')
output.setdefault(id, {})[date] = entry
dict.setdefault()
调用 returns 键 id
的值,但如果没有这样的键,第二个参数将被添加到 output
字典为该键并返回。
演示:
>>> input_list = [
... {'id':'1', 'date':'12312004','var1':'234', 'var2':'456'},
... {'id':'1', 'date':'12312005','var1':'200', 'var2':'466'},
... {'id':'1', 'date':'12312006','var1':'760', 'var2':'656'},
... {'id':'1', 'date':'12312007','var1':'215', 'var2':'256'},
... {'id':'2', 'date':'5312006','var1':'234', 'var2':'456'},
... {'id':'2', 'date':'5312007','var1':'200', 'var2':'466'},
... {'id':'2', 'date':'5312008','var1':'760', 'var2':'656'},
... {'id':'2', 'date':'5312009','var1':'215', 'var2':'256'},
... ]
>>> output = {}
>>> for entry in input_list:
... entry = entry.copy() # so we can alter it
... id = entry.pop('id')
... date = entry.pop('date')
... output.setdefault(id, {})[date] = entry
...
>>> from pprint import pprint
>>> pprint(output)
{'1': {'12312004': {'var1': '234', 'var2': '456'},
'12312005': {'var1': '200', 'var2': '466'},
'12312006': {'var1': '760', 'var2': '656'},
'12312007': {'var1': '215', 'var2': '256'}},
'2': {'5312006': {'var1': '234', 'var2': '456'},
'5312007': {'var1': '200', 'var2': '466'},
'5312008': {'var1': '760', 'var2': '656'},
'5312009': {'var1': '215', 'var2': '256'}}}
我有一个 .csv 文件,我使用 csv.dictwriter 将其读入 python,看起来像这样:
[
{'id':'1', 'date':'12312004','var1':'234', 'var2':'456'},
{'id':'1', 'date':'12312005','var1':'200', 'var2':'466'},
{'id':'1', 'date':'12312006','var1':'760', 'var2':'656'},
{'id':'1', 'date':'12312007','var1':'215', 'var2':'256'},
{'id':'2', 'date':'5312006','var1':'234', 'var2':'456'},
{'id':'2', 'date':'5312007','var1':'200', 'var2':'466'},
{'id':'2', 'date':'5312008','var1':'760', 'var2':'656'},
{'id':'2', 'date':'5312009','var1':'215', 'var2':'256'},
]
我想将其重组为:
{'1': {
'12312004': {'var1': '234', 'var2': '456'},
'12312005': {'var1': '200', 'var2': '466'},
'12312006': {'var1': '760', 'var2': '656'},
'12312007': {'var1': '215', 'var2': '256'}},
'2': {
'5312006': {'var1': '234', 'var2': '456'},
'5312007': {'var1': '200', 'var2': '466'},
'5312008': {'var1': '760', 'var2': '656'},
'5312009': {'var1': '215', 'var2': '256'}}
}
如果您输入的是一个列表,您可以这样做:
output = {}
for entry in input_list:
entry = entry.copy() # so we can mangle it
id = entry.pop('id')
date = entry.pop('date')
output.setdefault(id, {})[date] = entry
dict.setdefault()
调用 returns 键 id
的值,但如果没有这样的键,第二个参数将被添加到 output
字典为该键并返回。
演示:
>>> input_list = [
... {'id':'1', 'date':'12312004','var1':'234', 'var2':'456'},
... {'id':'1', 'date':'12312005','var1':'200', 'var2':'466'},
... {'id':'1', 'date':'12312006','var1':'760', 'var2':'656'},
... {'id':'1', 'date':'12312007','var1':'215', 'var2':'256'},
... {'id':'2', 'date':'5312006','var1':'234', 'var2':'456'},
... {'id':'2', 'date':'5312007','var1':'200', 'var2':'466'},
... {'id':'2', 'date':'5312008','var1':'760', 'var2':'656'},
... {'id':'2', 'date':'5312009','var1':'215', 'var2':'256'},
... ]
>>> output = {}
>>> for entry in input_list:
... entry = entry.copy() # so we can alter it
... id = entry.pop('id')
... date = entry.pop('date')
... output.setdefault(id, {})[date] = entry
...
>>> from pprint import pprint
>>> pprint(output)
{'1': {'12312004': {'var1': '234', 'var2': '456'},
'12312005': {'var1': '200', 'var2': '466'},
'12312006': {'var1': '760', 'var2': '656'},
'12312007': {'var1': '215', 'var2': '256'}},
'2': {'5312006': {'var1': '234', 'var2': '456'},
'5312007': {'var1': '200', 'var2': '466'},
'5312008': {'var1': '760', 'var2': '656'},
'5312009': {'var1': '215', 'var2': '256'}}}