【问题标题】:I am unable to create dictionary我无法创建字典
【发布时间】:2020-12-07 11:12:37
【问题描述】:

我无法创建结构如下的字典:

"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 然后循环它时遇到问题。我需要为多个 aggs 做这件事。 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}],
            },
        },
    ]

任何人都可以显示代码如何执行它。基本上我无法创建嵌套字典。

【问题讨论】:

  • 您遇到了什么问题?您实际上并没有告诉我们您看到的错误输出或错误代码。
  • 我没有收到任何错误。我无法使用以下信息创建像上面这样的字典。

标签: python python-3.x dictionary nested


【解决方案1】:

看起来您正在使用 JSON 数据。 你可以看到这个链接https://realpython.com/python-json/。 您可以使用内置的 JSON 库在 dict 中隐藏整个内容 基本上是这样的

json_string = """
{
    "researcher": {
        "name": "Ford Prefect",
        "species": "Betelgeusian",
        "relatives": [
            {
                "name": "Zaphod Beeblebrox",
                "species": "Betelgeusian"
            }
        ]
    }
}
"""
data = json.loads(json_string)

【讨论】:

  • 我需要使用下面的 json 文件创建一个 abouve dict。
  • 你可以看到我发布的链接。
【解决方案2】:

{} 中添加整个结构,并将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"}},
                    },
                }
            },
        }
    }}
{'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'}}}}}}}}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多