我和朋友一起为这个项目提供了这些数据:
arr = [{'Key':'Key 1', 'SecondKey':'SecondKey 1', 'ThirdKey':'Thirdkey 1', 'Value':100},
{'Key':'Key 1', 'SecondKey':'SecondKey 1', 'ThirdKey':'ThirdKey 2', 'Value':130},
{'Key':'Key 1', 'SecondKey':'SecondKey 2', 'ThirdKey':'ThirdKey 1', 'Value':230},
{'Key':'Key 1', 'SecondKey':'SecondKey 2', 'ThirdKey':'ThirdKey 2', 'Value':300},
{'Key':'Key 2', 'SecondKey':'SecondKey 4', 'ThirdKey':'ThirdKey 1', 'Value':111},
{'Key':'Key 2', 'SecondKey':'SecondKey 2', 'ThirdKey':'ThirdKey 2', 'Value':400},
{'Key':'Key 2', 'SecondKey':'SecondKey 1', 'ThirdKey':'ThirdKey 2', 'Value':230}
]
我尝试过这样的功能:
def array_to_dict(arr, classification):
dct = tree()
for d in arr:
dct[d["Key"]]=(d)
pprint.pprint(dct)
def tree():
return defaultdict(tree)
def array_to_dict_recursive(arr, classification):
result = defaultdict(arr)
for v, k in arr:
result[k].append(v)
final_result = [{'type': k, 'items': v} for k, v in result.items()]
print (str(final_result))
def array_cool(arr):
#pprint.pprint(arr)
arr.sort(key=operator.itemgetter('Key'))
pprint.pprint(arr)
list1= []
print("")
for key, items in itertools.groupby(arr, operator.itemgetter('Key')):
list1.append(list(items))
pprint.pprint(list1)
我希望它以这种方式显示为JSON文件:
{
"Key 1": {
"SecondKey 2": {
"ThirdKey 2": [
{
"Key": "Key 1",
"Value": 300,
"SecondKey": "SecondKey 2",
"ThirdKey": "ThirdKey 2"
}
],
"ThirdKey 1": [
{
"Key": "Key 1",
"Value": 230,
"SecondKey": "SecondKey 2",
"ThirdKey": "ThirdKey 1"
}
]
},
"SecondKey 1": {
"ThirdKey 2": [
{
"Key": "Key 1",
"Value": 130,
"SecondKey": "SecondKey 1",
"ThirdKey": "ThirdKey 2"
}
],
"ThirdKey 1": [
{
"Key": "Key 1",
"Value": 100,
"SecondKey": "SecondKey 1",
"ThirdKey": "ThirdKey 1"
}
]
}
},
"Key 2": {
"SecondKey 4": {
"ThirdKey 1": [
{
"Key": "Key 2",
"Value": 111,
"SecondKey": "SecondKey 4",
"ThirdKey": "ThirdKey 1"
}
]
},
"SecondKey 2": {
"ThirdKey 2": [
{
"Key": "Key 2",
"Value": 400,
"SecondKey": "SecondKey 2",
"ThirdKey": "ThirdKey 2"
}
]
},
"SecondKey 1": {
"ThirdKey 2": [
{
"Key": "Key 2",
"Value": 230,
"SecondKey": "SecondKey 1",
"ThirdKey": "ThirdKey 2"
}
]
}
}
}
我尝试了排序,但是排序将Key置于第二位并且混乱了下一个排序过程,并且它无法正常工作。
答案 0 :(得分:1)
尝试
print json.dumps(arr, indent=4, sort_keys=True)
有关详细信息,请参阅json.dumps文档:
json.dumps(obj,skipkeys = False,ensure_ascii = True,check_circular = True,allow_nan = True,cls = None,indent = None,separators = None,encoding =“utf-8”,default = None,sort_keys =假,** kw)
答案 1 :(得分:1)
Python dicts未分类。你有几个选择。
使用将排序应用于标准字典的工具,例如:
import json
print json.dumps(thedict, indent=4, sort_keys=True)
使用维护dict条目创建顺序的类型:
from collections import OrderedDict
d = OrderedDict()
d[KEY] = VALUE
d[KEY][SUBKEY] = VALUE
etc.
或者最简单的方法,只需按照您想要的顺序将它们添加到数组/列表中。
答案 2 :(得分:0)
这是你想要的第一个刺。它会让你开始,然后你可以简化它并使它漂亮。要点:
json.dumps()
按照看到的顺序输出字典键,因此显式排序的OrderedDict可能是您获得该字典的唯一方法。您的数据结构:
Key
做出决定,按顺序排序
SecondKey
的dict,反向排序
ThirdKey
的dict,反向排序
Key
值SecondKey
值ThirdKey
值
from collections import OrderedDict
import json
arr = [{'Key':'Key 1', 'SecondKey':'SecondKey 1', 'ThirdKey':'ThirdKey 1', 'Value':100},
{'Key':'Key 1', 'SecondKey':'SecondKey 1', 'ThirdKey':'ThirdKey 2', 'Value':130},
{'Key':'Key 1', 'SecondKey':'SecondKey 2', 'ThirdKey':'ThirdKey 1', 'Value':230},
{'Key':'Key 1', 'SecondKey':'SecondKey 2', 'ThirdKey':'ThirdKey 2', 'Value':300},
{'Key':'Key 2', 'SecondKey':'SecondKey 4', 'ThirdKey':'ThirdKey 1', 'Value':111},
{'Key':'Key 2', 'SecondKey':'SecondKey 2', 'ThirdKey':'ThirdKey 2', 'Value':400},
{'Key':'Key 2', 'SecondKey':'SecondKey 1', 'ThirdKey':'ThirdKey 2', 'Value':230}
]
def arr_to_ordereddict():
d = OrderedDict()
keys = set(a['Key'] for a in arr)
#print(sorted(keys))
for k1 in sorted(keys):
k1_dict = OrderedDict()
d[k1] = k1_dict
arr2 = [a for a in arr if a['Key'] == k1]
keys2 = set(a['SecondKey'] for a in arr2)
#print('\t', k1, sorted(keys2, reverse=True))
for k2 in sorted(keys2, reverse=True):
k2_dict = OrderedDict()
k1_dict[k2] = k2_dict
arr3 = [a for a in arr if a['SecondKey'] == k2]
keys3 = set(a['ThirdKey'] for a in arr3)
#print('\t\t', k1, k2, sorted(keys3, reverse=True))
for k3 in sorted(keys3, reverse=True):
k3_list = []
k2_dict[k3] = k3_list
for item in (a for a in arr3 if a['ThirdKey'] == k3):
value_dict = OrderedDict()
value_dict['Key'] = k1
value_dict['Value'] = item['Value']
value_dict['SecondKey'] = k2
value_dict['ThirdKey'] = k3
k3_list.append(value_dict)
return d
print(json.dumps(arr_to_ordereddict(), indent=4))