gpt4 book ai didi

python - 美化Python嵌套字典代码

转载 作者:行者123 更新时间:2023-12-03 02:22:22 25 4
gpt4 key购买 nike

输出:

{
"aggs": {
"overall": {
"date_histogram": {
"field": "created",
"calendar_interval": "day",
"time_zone": 3
},
"aggs": {
"series_attribute": {
"terms": {
"field": 2
},
"aggs": {
"types_count": {
"value_count": {
"field": 1
}
}
}
}
}
}
}
}

输入:
聚合1 =
{
"types_count": {
"value_count": {
"field": 1
}
}
}

聚合2 =
{
"series_attribute": {
"terms": {
"field": 2
}
}
}

聚合3 =
{
"overall": {
"date_histogram": {
"field": "created",
"calendar_interval": "day",
"time_zone": 3
}
}
}



countResponse,termResponse,dateResponse = {},{},{}
countResponse["aggs"] = aggregation1
termResponse["aggs"] = aggregation2
dateResponse["aggs"] = aggregation3
aggregation2["series_attribute"]["aggs"] = aggregation1
aggregation3["overall"]["aggs"] = termResponse["aggs"]
#print(json.dumps(dateResponse))

这段代码可以用,但是我不确定是否可以修改代码以使其看起来更好。我在另一个字典中嵌套了3个字典,但是我不喜欢我们访问键并添加键值对的方式。

最佳答案

您可以像这样更改代码以使其更具可读性。

output = { "aggs" : aggregation3 }
aggregation3["overall"]["aggs"] = aggregation2
aggregation2["series_attribute"]["aggs"] = aggregation1

关于python - 美化Python嵌套字典代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62038463/

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