gpt4 book ai didi

python - 从字典列表中动态创建嵌套 json 对象

转载 作者:太空宇宙 更新时间:2023-11-03 20:14:41 26 4
gpt4 key购买 nike

我想将字典列表转换为嵌套 JSON。我想动态填充字段。我想将所有信号分组为特定类别。我尝试了不同的方法,但无法生成所需的确切格式。需要一些逻辑帮助来生成 JSON

我的意见

signal_list = [
{'sig_id': 667, 'sig_name': 'RotorSpeed', 'Category': 'Gbox'},
{'sig_id': 672, 'sig_name': 'GeneratorSpeed', 'Category': 'Genarator'},
{'sig_id': 673, 'sig_name': 'NacelleSpeed', 'Category': 'Nacelle'},
{'sig_id': 668, 'sig_name': 'RotorDirection', 'Category': 'Gbox'}
]

我尝试过的代码:

d = defaultdict(list)
for t in signals:
field_list = d[t['Category']]
new_d = {'sig_name': t['sig_name'], 'sig_id': t['sig_id'], 'Category': t['Category']}
field_list.append(defaultdict(list, new_d))

电流输出

[
{
"Gbox":[
{
"sig_name":"RotorSpeed",
"sig_id":667
},
{
"sig_name":"RotorDirection",
"sig_id":668
}
]
},
{
"Genarator":[
{
"sig_name":"GeneratorSpeed",
"sig_id":672
}
]
},
{
"Signals":[
{
"sig_name":"NacelleSpeed",
"SignalID":673
}
]
}
]

(必需)我的输出应如下所示

[
{
"Category":"Gbox",
"Signals":[
{
"sig_name":"RotorSpeed",
"sig_id":667
},
{
"sig_name":"RotorDirection",
"sig_id":668
}
]
},
{
"Category":"Genarator",
"Signals":[
{
"sig_name":"GeneratorSpeed",
"sig_id":672
}
]
},
{
"Category":"Nacelle",
"Signals":[
{
"sig_name":"NacelleSpeed",
"SignalID":673
}
]
}
]

最佳答案

请参阅 itertools 文档,特别是 groupby功能。

import json
import itertools

dta = signal_list = [
{'sig_id': 667, 'sig_name': 'RotorSpeed', 'Category': 'Gbox'},
{'sig_id': 672, 'sig_name': 'GeneratorSpeed', 'Category': 'Genarator'},
{'sig_id': 673, 'sig_name': 'NacelleSpeed', 'Category': 'Nacelle'},
{'sig_id': 668, 'sig_name': 'RotorDirection', 'Category': 'Gbox'}
]

# this key is simply to be used on each dictionary to get the 'Category'
key = lambda dct: dct.get('Category')

# group works best with a sorted iterable. So let's sort by "Category' since that's
# your grouping key.
a = itertools.groupby(sorted(dta, key=key), key)

# groupby returns a generator like object that has tuples of (key, value)
# the value is also a generator like object that just has each item from the iterable
# that matches your grouping key. To get all the items we just turn them into a list
b = [{"Category": k, "Signals": list(v)} for k,v in a]

# using json to print it out in a nice format
print(json.dumps(b, indent=1))

输出:

[
{
"Category": "Gbox",
"Signals": [
{
"sig_id": 667,
"sig_name": "RotorSpeed",
"Category": "Gbox"
},
{
"sig_id": 668,
"sig_name": "RotorDirection",
"Category": "Gbox"
}
]
},
{
"Category": "Genarator",
"Signals": [
{
"sig_id": 672,
"sig_name": "GeneratorSpeed",
"Category": "Genarator"
}
]
},
{
"Category": "Nacelle",
"Signals": [
{
"sig_id": 673,
"sig_name": "NacelleSpeed",
"Category": "Nacelle"
}
]
}
]

如果我理解正确,您希望从列表中的每个词典中删除类别键。你可以这样做。如果您想删除多个键,只需编辑该函数并在内部执行某种循环:)

def remove_key(dct:dict, key):
"""
Simply takes a dictionary, copies it and remove the key of interest
from the copy and returns the copy.
"""
# This function will be used in a list comprehension so it has to
# return the dictionary and not just pop the key

dct_ = dct.copy()
_ = dct_.pop(key)

return dct_

b = [{"Category": k, "Signals": [remove_key(dct, 'Category') for dct in v]} for k,v in a]

print(json.dumps(b, indent=1))

输出

[
{
"Category": "Gbox",
"Signals": [
{
"sig_id": 667,
"sig_name": "RotorSpeed"
},
{
"sig_id": 668,
"sig_name": "RotorDirection"
}
]
},
{
"Category": "Genarator",
"Signals": [
{
"sig_id": 672,
"sig_name": "GeneratorSpeed"
}
]
},
{
"Category": "Nacelle",
"Signals": [
{
"sig_id": 673,
"sig_name": "NacelleSpeed"
}
]
}
]

关于python - 从字典列表中动态创建嵌套 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58530517/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com