【问题标题】:Delete a nested dictionary from a list of dictionary on a condition?根据条件从字典列表中删除嵌套字典?
【发布时间】:2021-11-08 06:33:06
【问题描述】:

我有以下带有嵌套字典的字典。如果键(对象名)中有苹果,我想删除嵌套字典,否则保留它。

输入

{
    "item": [
        {
            "seq_key": [
                {
                    "height": 153,
                    "object-name": "person:1"
                },
                {
                    "height": 107,
                    "object-name": "apple:1"
                },
                {
                    "height": 90,
                    "object-name": "orange:2"
                },
                {
                    "height": 84,
                    "object-name": "apple:3"
                },
                {
                    "height": 94,
                    "object-name": "apple:4"
                }
            ],
            "f-no": "0"
        },
        {
            "seq_key": [
                {
                    "height": 161,
                    "object-name": "person:1"
                },
                {
                    "height": 84,
                    "object-name": "mango:3"
                }
            ],
            "f-no": "1"
        }
    ]
}

我的预期输出

{
    "item": [
        {
            "seq_key": [
                {
                    "height": 153,
                    "object-name": "person:1"
                },
                {
                    "height": 90,
                    "object-name": "orange:2"
                }
            ],
            "f-no": "0"
        },
        {
            "seq_key": [
                {
                    "height": 161,
                    "object-name": "person:1"
                },
                {
                    "height": 84,
                    "object-name": "mango:3"
                }
            ],
            "f-no": "1"
        }
    ]
}

我尝试了什么

import json 

with open(r"my_file.json", encoding='utf-8') as f:
    abc = json.load(f)

for key,value in abc.items():
    for i in value:
        for n,item in enumerate(i.get('seq_key')):
            if item.get('object-name').split(":")[0]  == 'apple':
                item.clear()

上面的代码给出了空字典,我不知道如何从列表中删除。如果我尝试使用列表删除方法,只有两个项目被删除而不是全部。

谁能帮帮我,怎么去掉?

【问题讨论】:

    标签: python python-3.x dictionary


    【解决方案1】:

    您可以使用列表推导式创建一个包含您感兴趣的元素的新列表,而不是尝试删除列表元素:

    for seq in abc["item"]:
        seq["seq_key"] = [d for d in seq["seq_key"] if "apple" not in d["object-name"]]
    

    【讨论】:

      【解决方案2】:

      我认为您的代码大部分是正确的,除了尝试 clear 项目之外,您可能可以 pop 将该项目从列表中删除 -

      for key,value in abc.items():
          for i in value:
              for n,item in enumerate(i.get('seq_key')):
                  if item.get('object-name').split(":")[0]  == 'apple':
                      i.get('seq_key').pop(n)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-07-15
        • 2019-06-04
        • 2020-10-20
        • 2018-04-14
        • 2021-12-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多