如果给定的键和值从字典列表中匹配,则获取所有字典

时间:2019-08-19 11:41:34

标签: python python-3.x list dictionary

如果键和值的给定值匹配,我想从字典列表中获取字典。以下是我的输入和预期输出。

输入:

[
    {
        "sqlFile": "test.sql",
        "noOfStatements": 3,
        "allQueries": "queries",
        "sqlOperations": [
            {
                "type": "CreateTable",
                "objectName": "objectname1",
                "schemaName": null
            },
            {
                "type": "CreateTable",
                "objectName": "objectname2",
                "schemaName": null
            },
            {
                "type": "DROP",
                "objectName": "objectname3",
                "schemaName": null
            }
        ]
    }
]

预期输出:

[
            {
                "type": "CreateTable",
                "objectName": "objectname1",
                "schemaName": null
            },
            {
                "type": "CreateTable",
                "objectName": "objectname2",
                "schemaName": null
            }
]

json_data具有响应,而sql_operations具有上述指定的输入。以下无法正常工作。我需要帮助确定问题以获得预期的输出。

for i in json_data:
        logger.debug("I is :: {} ".format(type(i)))
        sql_operations = i['sqlOperations']
        for j in sql_operations:
            logger.debug("J is :: {} ".format(type(j)))
            for k,v in j.items():
                if k == 'operationType' and v == 'REPLACE View':
                     logger.debug("Key is {} and Item is {}".format(k,v))

5 个答案:

答案 0 :(得分:1)

作为单个列表理解者,您可以做到

output = [d for i in json_data for d in i['sqlOperations'] if d['type'] == 'CreateTable']

或对循环使用标准

output = []
for i in json_data:
    for d in i['sqlOperations']:
        if d['type'] == 'CreateTable':
            output.append(d)

答案 1 :(得分:0)

假设您使用json.load将输入作为dict的python列表获取,您可以使用列表理解吗?

for i in json_data:
    ops = i.get('sqlOperations', [])
    select_dicts = [d for d in ops if d.get('type') == 'CreateTable']
    new_list.extend(select_dicts)

new_list
[{'type': 'CreateTable', 'objectName': 'objectname1', 'schemaName': None}, 
{'type': 'CreateTable', 'objectName': 'objectname2', 'schemaName': None}]

答案 2 :(得分:0)

x = """ [{
    "sqlFile":
    "test.sql",
    "noOfStatements":
    3,
    "allQueries":
    "queries",
    "sqlOperations": [{
        "type": "CreateTable",
        "objectName": "objectname1",
        "schemaName": null
    }, {
        "type": "CreateTable",
        "objectName": "objectname2",
        "schemaName": null
    }, {
        "type": "DROP",
        "objectName": "objectname3",
        "schemaName": null
    }]
}]"""
import json
x = json.loads(x)
a = x[0]["sqlOperations"][0:2]
print(json.dumps(a, indent=2))

输出

[
  {
    "type": "CreateTable",
    "objectName": "objectname1",
    "schemaName": null
  },
  {
    "type": "CreateTable",
    "objectName": "objectname2",
    "schemaName": null
  }
]

答案 3 :(得分:0)

final_data = []
for j in sql_operations:
    if j.get("type") == "CreateTable":
        final_data.append(j)

注意:null在Python中不是有效的关键字。应该是None

答案 4 :(得分:0)

假设这是字典(尽管null表示json数据)...

我已经自由地将null更改为None,因为null会抛出NameError。这里的列表理解也会产生一个列表列表,因此要获得输出,只需选择第一个元素,但是如果输入列表中有多个元素,它将起作用。

json_data = [
    {
        "sqlFile": "test.sql",
        "noOfStatements": 3,
        "allQueries": "queries",
        "sqlOperations": [
            {
                "type": "CreateTable",
                "objectName": "objectname1",
                "schemaName": None
            },
            {
                "type": "CreateTable",
                "objectName": "objectname2",
                "schemaName": None
            },
            {
                "type": "DROP",
                "objectName": "objectname3",
                "schemaName": None
            }
        ]
    }
]

selected_data = [[v for v in jd['sqlOperations'] if v.get('type') == 'CreateTable'] for jd in json_data if jd.get('sqlOperations')]
print(selected_data[0])

结果

[{'type': 'CreateTable', 'objectName': 'objectname1', 'schemaName': None}, {'type': 'CreateTable', 'objectName': 'objectname2', 'schemaName': None}]