Python 我无法创建字典
我无法创建结构如下的词典:Python 我无法创建字典,python,python-3.x,dictionary,nested,Python,Python 3.x,Dictionary,Nested,我无法创建结构如下的词典: "aggs": { "4": { "date_histogram": { "field": "@timestamp", "interval": "1m", "time_zone"
"aggs": {
"4": {
"date_histogram": {
"field": "@timestamp",
"interval": "1m",
"time_zone": "Asia/Kolkata",
"min_doc_count": 1,
},
"aggs": {
"5": {
"range": {
"field": "system.core.idle.pct",
"ranges": [{"from": 0, "to": 0.001}, {"from": 0.001, "to": 1}],
"keyed": true,
},
"aggs": {
"2": {"max": {"field": "system.core.system.pct"}},
"3": {"max": {"field": "system.core.idle.pct"}},
},
}
},
}
}
主要是我在aggs:4中创建aggs:5,然后循环它时遇到了问题。我需要为多个AGG执行此操作。agg id的数量也可以高达1000
我试图从字典中推导出:
"aggs": [
{"id": "1", "enabled": true, "type": "count", "schema": "metric", "params": {}},
{
"id": "2",
"enabled": true,
"type": "max",
"schema": "metric",
"params": {"field": "system.core.system.pct", "customLabel": "system"},
},
{
"id": "3",
"enabled": true,
"type": "max",
"schema": "metric",
"params": {"field": "system.core.idle.pct", "customLabel": "idle"},
},
{
"id": "4",
"enabled": true,
"type": "date_histogram",
"schema": "bucket",
"params": {
"field": "@timestamp",
"interval": "m",
"customInterval": "2h",
"min_doc_count": 1,
"extended_bounds": {},
},
},
{
"id": "5",
"enabled": true,
"type": "range",
"schema": "bucket",
"params": {
"field": "system.core.idle.pct",
"ranges": [{"from": 0, "to": 0.001}, {"from": 0.001, "to": 1}],
},
},
]
任何人都可以演示代码如何执行它。基本上,我无法创建嵌套字典。看起来您正在使用JSON数据。 你可以看到这个链接。 您可以使用内置的JSON库在dict中隐藏整个内容 基本上是这样的
json_string = """
{
"researcher": {
"name": "Ford Prefect",
"species": "Betelgeusian",
"relatives": [
{
"name": "Zaphod Beeblebrox",
"species": "Betelgeusian"
}
]
}
}
"""
data = json.loads(json_string)
在
{}
中添加整个结构,并将true
替换为true
{"aggs": {
"4": {
"date_histogram": {
"field": "@timestamp",
"interval": "1m",
"time_zone": "Asia/Kolkata",
"min_doc_count": 1,
},
"aggs": {
"5": {
"range": {
"field": "system.core.idle.pct",
"ranges": [{"from": 0, "to": 0.001}, {"from": 0.001, "to": 1}],
"keyed": True,
},
"aggs": {
"2": {"max": {"field": "system.core.system.pct"}},
"3": {"max": {"field": "system.core.idle.pct"}},
},
}
},
}
}}
你有什么问题?你还没有告诉我们你看到了什么错误的输出或错误代码。我没有收到任何错误。我无法使用下面的信息创建像上面这样的字典。我需要使用下面的json文件创建abouve dict。你可以看到我发布的链接。
{'aggs': {'4': {'date_histogram': {'field': '@timestamp',
'interval': '1m',
'time_zone': 'Asia/Kolkata',
'min_doc_count': 1},
'aggs': {'5': {'range': {'field': 'system.core.idle.pct',
'ranges': [{'from': 0, 'to': 0.001}, {'from': 0.001, 'to': 1}],
'keyed': True},
'aggs': {'2': {'max': {'field': 'system.core.system.pct'}},
'3': {'max': {'field': 'system.core.idle.pct'}}}}}}}}