spritevan
2014-09-12 16:16:18 +08:00
#!/usr/bin/env python
from pprint import pprint as pp
origin = {
"A0801_000000_201301": "1,321.8",
"A0801_000000_201302": "1,199.8",
"A0801_000000_201309": "1,433.4",
"A0802_000000_201305": "6,688.3",
"A0802_000000_201306": "8,085.2",
"A0802_000000_201307": "9,481.0",
"A0802_000000_201308": "10,878.4",
"A0802_000000_201309": "12,311.8",
"A0802_000000_201310": "13,739.9",
}
res = {}
fn = lambda fields,v: res.setdefault(fields[0], []).append({fields[-1]:v})
for k,v in origin.iteritems():
fn(k.split('_'),v)
pp(res)
---
{'A0801': [{'201309': '1,433.4'},
{'201302': '1,199.8'},
{'201301': '1,321.8'}],
'A0802': [{'201305': '6,688.3'},
{'201306': '8,085.2'},
{'201307': '9,481.0'},
{'201310': '13,739.9'},
{'201308': '10,878.4'},
{'201309': '12,311.8'}]}