Python 如果给定的键和值与字典列表中的匹配,则获取所有字典
如果键和值的给定值匹配,我想从dict列表中获取dict。下面是我的输入和预期输出Python 如果给定的键和值与字典列表中的匹配,则获取所有字典,python,python-3.x,list,dictionary,Python,Python 3.x,List,Dictionary,如果键和值的给定值匹配,我想从dict列表中获取dict。下面是我的输入和预期输出 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 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))
输入:
[
{
"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_数据具有响应,sql_操作具有上述指定的输入。以下选项未按预期工作。我需要帮助确定问题以获得预期输出
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))
假设您使用json.load以python dict列表的形式获取输入,那么您可以使用列表理解来实现这一点吗
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}]
作为一个单一的列表,你可以做什么
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)
输出
[
{
"type": "CreateTable",
"objectName": "objectname1",
"schemaName": null
},
{
"type": "CreateTable",
"objectName": "objectname2",
"schemaName": null
}
]
注意:null在Python中不是有效的关键字。假设这是一个dict,尽管为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}]
你能发布错误/输出你的gettingWhat是null应该在你的输入中,None,或者null吗?
[{'type': 'CreateTable', 'objectName': 'objectname1', 'schemaName': None}, {'type': 'CreateTable', 'objectName': 'objectname2', 'schemaName': None}]