인생사는 이야기
[python] 사전형 자료 재배열 하기 본문
1. 아래 Dict 자료를 시간 기준으로 재배열 하기
2. 시간은 마지막 초0으로 변경.
ex)1489998707 --> 1489998700
1489998709 --> 1489998700
3. 불필요한 자료 제거(result['ns'])
4. 시간별로 value 정렬을 위해 새로운 Dict 생성(unixtimestamp -> yyyy/mm/dd hh:mm:ss 형식으로 변환)
5. 시간별 Dict에 itemid별 value값 추가.
result = [{"itemid": "584507","clock": "1489998707","value": "1525935000","ns": "274653090"},
{"itemid": "584509","clock": "1489998709","value": "1180665000","ns": "663024554"},
{"itemid": "584525","clock": "1489998725","value": "2065962000","ns": "571889466"},
{"itemid": "584527","clock": "1489998727","value": "2043235000","ns": "963580065"},
{"itemid": "584529","clock": "1489998729","value": "85880000","ns": "363697189"},
{"itemid": "584507","clock": "1489999007","value": "1460716000","ns": "584114288"},
{"itemid": "584509","clock": "1489999009","value": "1180270000","ns": "446642803"},
{"itemid": "584525","clock": "1489999025","value": "2129115000","ns": "355208260"},
{"itemid": "584527","clock": "1489999027","value": "1987710000","ns": "987599192"},
{"itemid": "584529","clock": "1489999029","value": "81890000","ns": "204178562"},
{"itemid": "584507","clock": "1489999307","value": "1457121000","ns": "275057487"},
{"itemid": "584509","clock": "1489999309","value": "1121705000","ns": "582914403"},
{"itemid": "584525","clock": "1489999325","value": "2069600000","ns": "836550194"},
{"itemid": "584527","clock": "1489999328","value": "2074828000","ns": "221855625"},
{"itemid": "584529","clock": "1489999329","value": "67525000","ns": "146672189"},
{"itemid": "584507","clock": "1489999607","value": "1381262000","ns": "40991380"},
{"itemid": "584509","clock": "1489999609","value": "1165791000","ns": "714677108"},
{"itemid": "584525","clock": "1489999625","value": "2142353000","ns": "442754644"},
{"itemid": "584527","clock": "1489999627","value": "2263830000","ns": "852858860"},
{"itemid": "584529","clock": "1489999629","value": "82332000","ns": "198922812"},
{"itemid": "584507","clock": "1489999907","value": "1440140000","ns": "504845518"},
{"itemid": "584509","clock": "1489999909","value": "1157278000","ns": "259017403"},
{"itemid": "584525","clock": "1489999925","value": "2346162000","ns": "865844133"},
{"itemid": "584527","clock": "1489999928","value": "2021452000","ns": "239526195"},
{"itemid": "584529","clock": "1489999929","value": "70456000","ns": "146779781"},
{"itemid": "584507","clock": "1490000207","value": "1434792000","ns": "155763366"},
{"itemid": "584509","clock": "1490000209","value": "1154717000","ns": "198453008"},
{"itemid": "584525","clock": "1490000225","value": "2091908000","ns": "348910103"},
{"itemid": "584527","clock": "1490000227","value": "2037546000","ns": "507799012"},
{"itemid": "584529","clock": "1490000229","value": "76292000","ns": "233255303"},
{"itemid": "584507","clock": "1490000507","value": "1435598000","ns": "733737106"},
{"itemid": "584509","clock": "1490000509","value": "1089969000","ns": "404456133"},
{"itemid": "584525","clock": "1490000525","value": "2113838000","ns": "739621809"},
{"itemid": "584527","clock": "1490000528","value": "1808868000","ns": "188432109"},
{"itemid": "584529","clock": "1490000529","value": "80179000","ns": "113507270"}
]
dic = {}
for i in range(len(result)):
clock = result[i].get('clock')[:-1]
clock = clock + "0"
del result[i]['ns']
result[i].update({'clock': clock})
clock = result[i].get('clock')
Date = int(result[i].get('clock'))
date = datetime.datetime.fromtimestamp(Date).strftime('%Y/%m/%d %H:%M:%S')
if clock not in dic:
dic[clock] = {'clock': date, result[i].get('itemid'): result[i].get('value')}
else:
dic[clock][result[i].get('itemid')] = result[i].get('value')
print type(dic)
aa = sorted(dic.items())
for i in aa:
print i[1]
결과값
<type 'dict'>
{'584509': '1180665000', '584507': '1525935000', 'clock': '2017/03/20 17:31:40'}
{'584527': '2043235000', '584525': '2065962000', '584529': '85880000', 'clock': '2017/03/20 17:32:00'}
{'584509': '1180270000', '584507': '1460716000', 'clock': '2017/03/20 17:36:40'}
{'584527': '1987710000', '584525': '2129115000', '584529': '81890000', 'clock': '2017/03/20 17:37:00'}
{'584509': '1121705000', '584507': '1457121000', 'clock': '2017/03/20 17:41:40'}
{'584527': '2074828000', '584525': '2069600000', '584529': '67525000', 'clock': '2017/03/20 17:42:00'}
{'584509': '1165791000', '584507': '1381262000', 'clock': '2017/03/20 17:46:40'}
{'584527': '2263830000', '584525': '2142353000', '584529': '82332000', 'clock': '2017/03/20 17:47:00'}
{'584509': '1157278000', '584507': '1440140000', 'clock': '2017/03/20 17:51:40'}
{'584527': '2021452000', '584525': '2346162000', '584529': '70456000', 'clock': '2017/03/20 17:52:00'}
{'584509': '1154717000', '584507': '1434792000', 'clock': '2017/03/20 17:56:40'}
{'584527': '2037546000', '584525': '2091908000', '584529': '76292000', 'clock': '2017/03/20 17:57:00'}
{'584509': '1089969000', '584507': '1435598000', 'clock': '2017/03/20 18:01:40'}
{'584527': '1808868000', '584525': '2113838000', '584529': '80179000', 'clock': '2017/03/20 18:02:00'}
'IT > PYTHON' 카테고리의 다른 글
Python 리스트 안에 딕셔너리 정렬 (0) | 2023.02.02 |
---|---|
[Python] 파이썬 배우기 (1) | 2012.03.29 |