python2创立有序字典

    xiaoxiao2024-04-17  8

    1. 直接创建有序字典

    import collections data = collections.OrderedDict() data["a"] = "1" data["b"] = "2" data["c"] = "3" print data # 输出:OrderedDict([('a', '1'), ('b', '2'), ('c', '3')])

    2. 将json字符串按字符串顺序转成有序字典

    import json from collections import OrderedDict jsonStr = '{"a":1,"b":2,"c":3}' configDict = json.loads(jsonStr, object_pairs_hook=OrderedDict) print configDict # 输出:OrderedDict([(u'a', 1), (u'b', 2), (u'c', 3)])

     

    最新回复(0)