算法 - Python如何将数据库导出的数据一级级分类?
PHPz
PHPz 2017-04-18 09:49:59
0
2
383
lst = [{'time': '2016-10-25', 'id': 'a1', 'type': '1', 'details': 'xxx'},
       {'time': '2016-10-25', 'id': 'a1', 'type': '1', 'details': 'xxx'},
       {'time': '2016-10-25', 'id': 'a2', 'type': '1', 'details': 'xxx'},
       {'time': '2016-10-25', 'id': 'a2', 'type': '2', 'details': 'xxx'},
       {'time': '2016-10-26', 'id': 'a1', 'type': '1', 'details': 'xxx'},
       {'time': '2016-10-26', 'id': 'a1', 'type': '2', 'details': 'xxx'},
       {'time': '2016-10-26', 'id': 'a2', 'type': '1', 'details': 'xxx'},
       {'time': '2016-10-26', 'id': 'a2', 'type': '2', 'details': 'xxx'},
       {'time': '2016-10-27', 'id': 'a1', 'type': '1', 'details': 'xxx'},
       {'time': '2016-10-27', 'id': 'a1', 'type': '1', 'details': 'xxx'},
       {'time': '2016-10-27', 'id': 'a2', 'type': '2', 'details': 'xxx'},
       {'time': '2016-10-27', 'id': 'a2', 'type': '2', 'details': 'xxx'},
       {'time': '2016-10-27', 'id': 'a2', 'type': '3', 'details': 'xxx'},
       {'time': '2016-10-27', 'id': 'a2', 'type': '3', 'details': 'xxx'}]

结果:

lst = [{'time': '2016-10-25', 'data': [{'id': 'a1', 'data': [{'type': '1', 'data': [{'details': 'xxx'}, {'details': 'xxx'}]}]},
                                       {'id': 'a2', 'data': [{'type': '1', 'data': [{'details': 'xxx'}]},
                                                             {'type': '2', 'data': [{'details': 'xxx'}]}]}]},
       {'time': '2016-10-26', 'data': [{'id': 'a1', 'data': [{'type': '1', 'data': [{'details': 'xxx'}]},
                                                             {'type': '2', 'data': [{'details': 'xxx'}]}]},
                                       {'id': 'a2', 'data': [{'type': '1', 'data': [{'details': 'xxx'}]},
                                                             {'type': '2', 'data': [{'details': 'xxx'}]}]}]},
       {'time': '2016-10-27', 'data': [{'id': 'a1', 'data': [{'type': '1', 'data': [{'details': 'xxx'}, {'details': 'xxx'}]}]},
                                       {'id': 'a2', 'data': [{'type': '2', 'data': [{'details': 'xxx'}, {'details': 'xxx'}]},
                                                             {'type': '3', 'data': [{'details': 'xxx'}, {'details': 'xxx'}]}]}]}]

就是对列表中的数据进行一级级分类,原始数据是随机的,先按time分类,后面按id,然后type分类,每一级的数据都放在data的下,data是一个列表

真实的数据量比较大,怎么高效写这段程序?Python代码,谢谢

PHPz
PHPz

学习是最好的投资!

全部回覆(2)
Ty80

雷雷

刘奇

是 SQL 語句問題吧,如果不是那麼你得賦值很多次變量,互換值。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板