【问题标题】:Get the dictionary on a list where there is a specific key value pairs在有特定键值对的列表中获取字典
【发布时间】:2021-02-10 09:07:55
【问题描述】:

假设我在一个列表中有这本字典:

 [
    {
        "Name": "Person",
        "Confidence": 97.56156921386719,
        "Instances": [
            {
                "BoundingBox": {
                    "Width": 0.6137702465057373,
                    "Height": 0.9175498485565186,
                    "Left": 0.22297996282577515,
                    "Top": 0.0739903450012207
                },
                "Confidence": 94.51961517333984
            },
            {
                "BoundingBox": {
                    "Width": 0.46570318937301636,
                    "Height": 0.6405649781227112,
                    "Left": 0.11447866261005402,
                    "Top": 0.34079012274742126
                },
                "Confidence": 82.2153549194336
            }
        ],
        "Parents": []
    },
    {
        "Name": "Human",
        "Confidence": 97.56156921386719,
        "Instances": [],
        "Parents": []
    },
    {
        "Name": "Clothing",
        "Confidence": 91.08417510986328,
        "Instances": [],
        "Parents": []
    }
    }]

我怎样才能只检索键值为 "Name":"Clothing" 的字典?是否可以从字典中检索它? “服装”键可以出现在列表的任何部分。

    {
        "Name": "Clothing",
        "Confidence": 91.08417510986328,
        "Instances": [],
        "Parents": []
    }
  

【问题讨论】:

    标签: python json list dictionary


    【解决方案1】:

    你可以使用这个简单的list comprehension:

    [print(dictionary) for dictionary in lst if 'Name' in dictionary.keys() and dictionary['Name'] == 'Clothing']
    

    输出:

    {'Name': 'Clothing', 'Confidence': 91.08417510986328, 'Instances': [], 'Parents': []}
    

    这里是完整的代码:

    lst =  [
        {
            "Name": "Person",
            "Confidence": 97.56156921386719,
            "Instances": [
                {
                    "BoundingBox": {
                        "Width": 0.6137702465057373,
                        "Height": 0.9175498485565186,
                        "Left": 0.22297996282577515,
                        "Top": 0.0739903450012207
                    },
                    "Confidence": 94.51961517333984
                },
                {
                    "BoundingBox": {
                        "Width": 0.46570318937301636,
                        "Height": 0.6405649781227112,
                        "Left": 0.11447866261005402,
                        "Top": 0.34079012274742126
                    },
                    "Confidence": 82.2153549194336
                }
            ],
            "Parents": []
        },
        {
            "Name": "Human",
            "Confidence": 97.56156921386719,
            "Instances": [],
            "Parents": []
        },
        {
            "Name": "Clothing",
            "Confidence": 91.08417510986328,
            "Instances": [],
            "Parents": []
        }
    ]
    
    [print(dictionary) for dictionary in lst if 'Name' in dictionary.keys() and dictionary['Name'] == 'Clothing']
    

    【讨论】:

    • 这个有什么pythonic风格吗?还是这样更好?
    • 您可以使用我上面提到的列表理解。列表推导更 Pythonic
    • 我不会在列表推导中打印,但列表推导是过滤列表的典型方式。
    • 是的...我同意。但是在列表推导中打印元素是不是一种不太好的编码方式?
    • 我投了赞成票;我要做的一个小改动是用dictionary.get("Name") == "Clothing" 替换列表理解中的条件。
    【解决方案2】:

    假设您的列表名为 my_list,那么这应该可以满足您的需求:

    my_list_new = [x for x in my_list if ("Name", "Clothing") in x.items()]

    【讨论】:

    • 几分钟后将被标记为答案,我选择这个是因为它比之前的答案短
    • 投反对票。 if ("Name", "Clothing") in x.items() 不必要地低效且不可读。它忽略了使用字典的全部意义。
    • @blhsing 同意。对于那些想知道为什么它效率低下的人。这个列表理解必须为 my_list 中的每个字典 x 调用 x.items()。它会在每个结果中线性搜索元组(“Name”、“Clothing”)。对于所有没有该元组的字典,它会读取每个键、值对以选择不包含 x。
    • 我明白了。感谢您的反馈
    【解决方案3】:

    您可以使用以下代码获取具有NameClothing 的每个项目:

    names = [
        {
            "Name": "Person",
            "Confidence": 97.56156921386719,
            "Instances": [
                {
                    "BoundingBox": {
                        "Width": 0.6137702465057373,
                        "Height": 0.9175498485565186,
                        "Left": 0.22297996282577515,
                        "Top": 0.0739903450012207
                    },
                    "Confidence": 94.51961517333984
                },
                {
                    "BoundingBox": {
                        "Width": 0.46570318937301636,
                        "Height": 0.6405649781227112,
                        "Left": 0.11447866261005402,
                        "Top": 0.34079012274742126
                    },
                    "Confidence": 82.2153549194336
                }
            ],
            "Parents": []
        },
        {
            "Name": "Human",
            "Confidence": 97.56156921386719,
            "Instances": [],
            "Parents": []
        },
        {
            "Name": "Clothing",
            "Confidence": 91.08417510986328,
            "Instances": [],
            "Parents": []
        }
    ]
    
    for name in names:
        if name['Name'] == 'Clothing':
            print(name)
    
    

    如果你希望它简短,你可以使用以下代码:

    x = [y for y in names if name['Name'] == 'Clothing']
    

    【讨论】:

      【解决方案4】:

      这里可以使用.get(Key, defaultValue)

      .get("Name", "") --> 如果字典中不存在键 Name,则返回一个空字符串。 doc .get("Name") --> 如果Name 不存在,则返回None

      所以.get("Name") == "Clothing"是选择字典的标准。

      并使用filter() 仅选择满足条件的那些。

      把它们放在一起

      filter(lambda x: x.get("Name") == "Clothing", mylist)
      

      filter 返回一个迭代器,因此您可以将它传递给 list 以创建完整列表。 如果你只是想遍历它,不需要调用列表。

      所以,

      list(filter(lambda x: x.get("Name") == "Clothing", mylist))
      

      【讨论】:

        猜你喜欢
        • 2017-10-23
        • 2020-03-14
        • 2019-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多