![](/img/trans.png)
[英]Python - Remove dictionary from list of dictionaries if list of strings not in dictionary key
[英]remove dictionaries from list based on key value from nested dictionary in python
我有以下字典列表:
item = {
"relationship": [
{
"name": "",
"id": "",
"type": "",
"relationship": {
"id": "3006:33",
"fkey_logical_column_id": "2006:41",
"pkey_logical_column_id": "2006:51",
"fkey_column_id": "3003:9",
"fkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"ORDER_ITEMS\".\"ORDER_ID\"",
"pkey_column_id": "3003:17",
"pkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"ORDERS\".\"ORDER_ID\"",
"fkey_table_id": "3001:7",
"pkey_table_id": "3001:14",
"pkey_physical_table_name": "",
"pkey_business_table_name": "",
"type": "",
"fkey_business_column_name": "\"ofline_online_test\".\"ORDER_ITEMS\".\"ORDER_ID\"",
"pkey_business_column_name": "\"ofline_online_test\".\"ORDERS\".\"ORDER_ID\"",
"from_type": "0..1",
"to_type": "n..n"
}
},
{
"name": "",
"id": "",
"type": "",
"relationship": {
"id": "3006:34",
"fkey_logical_column_id": "2006:42",
"pkey_logical_column_id": "2006:70",
"fkey_column_id": "3003:10",
"fkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"ORDER_ITEMS\".\"PRODUCT_ID\"",
"pkey_column_id": "3003:29",
"pkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"PRODUCTS\".\"PRODUCT_ID\"",
"fkey_table_id": "3001:7",
"pkey_table_id": "3001:25",
"pkey_physical_table_name": "",
"pkey_business_table_name": "",
"type": "",
"fkey_business_column_name": "\"ofline_online_test\".\"ORDER_ITEMS\".\"PRODUCT_ID\"",
"pkey_business_column_name": "\"ofline_online_test\".\"PRODUCTS\".\"PRODUCT_ID\"",
"from_type": "0..1",
"to_type": "n..n"
}
},
{
"name": "",
"id": "",
"type": "",
"relationship": {
"id": "3006:35",
"fkey_logical_column_id": "2006:67",
"pkey_logical_column_id": "2006:61",
"fkey_column_id": "3003:26",
"fkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"PRODUCTS\".\"CATEGORY_ID\"",
"pkey_column_id": "3003:22",
"pkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"PRODUCT_CATEGORIES\".\"CATEGORY_ID\"",
"fkey_table_id": "3001:25",
"pkey_table_id": "3001:21",
"pkey_physical_table_name": "",
"pkey_business_table_name": "",
"type": "",
"fkey_business_column_name": "\"ofline_online_test\".\"PRODUCTS\".\"CATEGORY_ID\"",
"pkey_business_column_name": "\"ofline_online_test\".\"PRODUCT_CATEGORIES\".\"CATEGORY_ID\"",
"from_type": "0..1",
"to_type": "n..n"
}
}
]
}
我有一個搜索 id **pkey_table_id = 3001:14 **,它出現在列表項 ["relationship"][0]["relationship"]["pkey_table_id"] 的第一項中。 我想用 key item["relationship"] 遍歷列表,如果找到匹配項 **pkey_table_id = 3001:14 ** 我想從列表中刪除該特定項目。所以我的結果將是 output
item = {
"relationship": [
{
"name": "",
"id": "",
"type": "",
"relationship": {
"id": "3006:34",
"fkey_logical_column_id": "2006:42",
"pkey_logical_column_id": "2006:70",
"fkey_column_id": "3003:10",
"fkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"ORDER_ITEMS\".\"PRODUCT_ID\"",
"pkey_column_id": "3003:29",
"pkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"PRODUCTS\".\"PRODUCT_ID\"",
"fkey_table_id": "3001:7",
"pkey_table_id": "3001:25",
"pkey_physical_table_name": "",
"pkey_business_table_name": "",
"type": "",
"fkey_business_column_name": "\"ofline_online_test\".\"ORDER_ITEMS\".\"PRODUCT_ID\"",
"pkey_business_column_name": "\"ofline_online_test\".\"PRODUCTS\".\"PRODUCT_ID\"",
"from_type": "0..1",
"to_type": "n..n"
}
},
{
"name": "",
"id": "",
"type": "",
"relationship": {
"id": "3006:35",
"fkey_logical_column_id": "2006:67",
"pkey_logical_column_id": "2006:61",
"fkey_column_id": "3003:26",
"fkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"PRODUCTS\".\"CATEGORY_ID\"",
"pkey_column_id": "3003:22",
"pkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"PRODUCT_CATEGORIES\".\"CATEGORY_ID\"",
"fkey_table_id": "3001:25",
"pkey_table_id": "3001:21",
"pkey_physical_table_name": "",
"pkey_business_table_name": "",
"type": "",
"fkey_business_column_name": "\"ofline_online_test\".\"PRODUCTS\".\"CATEGORY_ID\"",
"pkey_business_column_name": "\"ofline_online_test\".\"PRODUCT_CATEGORIES\".\"CATEGORY_ID\"",
"from_type": "0..1",
"to_type": "n..n"
}
}
]
}
這是我嘗試過的:
id_input = 3001:14
item ['relationship'] = list(filter(lambda i: i['pkey_table_id'] != id_input , item['relationship']['relationship']))
以下應該有效:
id_input = '3001:14'
item["relationship"] = [
i for i in item["relationship"] if i["relationship"]["pkey_table_id"] != id_input
]
你幾乎是對的,稍微更正了你的代碼:
def process(item):
id_input = '3001:14'
item['relationship'] = list(filter(lambda i:
i['relationship']['pkey_table_id'] != id_input , item['relationship']))
item = {
"relationship": [
{
"name": "",
"id": "",
"type": "",
"relationship": {
"id": "3006:33",
"fkey_logical_column_id": "2006:41",
"pkey_logical_column_id": "2006:51",
"fkey_column_id": "3003:9",
"fkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"ORDER_ITEMS\".\"ORDER_ID\"",
"pkey_column_id": "3003:17",
"pkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"ORDERS\".\"ORDER_ID\"",
"fkey_table_id": "3001:7",
"pkey_table_id": "3001:14",
"pkey_physical_table_name": "",
"pkey_business_table_name": "",
"type": "",
"fkey_business_column_name": "\"ofline_online_test\".\"ORDER_ITEMS\".\"ORDER_ID\"",
"pkey_business_column_name": "\"ofline_online_test\".\"ORDERS\".\"ORDER_ID\"",
"from_type": "0..1",
"to_type": "n..n"
}
},
{
"name": "",
"id": "",
"type": "",
"relationship": {
"id": "3006:34",
"fkey_logical_column_id": "2006:42",
"pkey_logical_column_id": "2006:70",
"fkey_column_id": "3003:10",
"fkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"ORDER_ITEMS\".\"PRODUCT_ID\"",
"pkey_column_id": "3003:29",
"pkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"PRODUCTS\".\"PRODUCT_ID\"",
"fkey_table_id": "3001:7",
"pkey_table_id": "3001:25",
"pkey_physical_table_name": "",
"pkey_business_table_name": "",
"type": "",
"fkey_business_column_name": "\"ofline_online_test\".\"ORDER_ITEMS\".\"PRODUCT_ID\"",
"pkey_business_column_name": "\"ofline_online_test\".\"PRODUCTS\".\"PRODUCT_ID\"",
"from_type": "0..1",
"to_type": "n..n"
}
},
{
"name": "",
"id": "",
"type": "",
"relationship": {
"id": "3006:35",
"fkey_logical_column_id": "2006:67",
"pkey_logical_column_id": "2006:61",
"fkey_column_id": "3003:26",
"fkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"PRODUCTS\".\"CATEGORY_ID\"",
"pkey_column_id": "3003:22",
"pkey_column_name": "\"localhost:1521/orcl\"..\"OT\".\"PRODUCT_CATEGORIES\".\"CATEGORY_ID\"",
"fkey_table_id": "3001:25",
"pkey_table_id": "3001:21",
"pkey_physical_table_name": "",
"pkey_business_table_name": "",
"type": "",
"fkey_business_column_name": "\"ofline_online_test\".\"PRODUCTS\".\"CATEGORY_ID\"",
"pkey_business_column_name": "\"ofline_online_test\".\"PRODUCT_CATEGORIES\".\"CATEGORY_ID\"",
"from_type": "0..1",
"to_type": "n..n"
}
}
]
}
process(item)
print(item)
Output:
{
"relationship": [
{
"name": "",
"id": "",
"type": "",
"relationship": {
"id": "3006:34",
"fkey_logical_column_id": "2006:42",
"pkey_logical_column_id": "2006:70",
"fkey_column_id": "3003:10",
"fkey_column_name": '"localhost:1521/orcl".."OT"."ORDER_ITEMS"."PRODUCT_ID"',
"pkey_column_id": "3003:29",
"pkey_column_name": '"localhost:1521/orcl".."OT"."PRODUCTS"."PRODUCT_ID"',
"fkey_table_id": "3001:7",
"pkey_table_id": "3001:25",
"pkey_physical_table_name": "",
"pkey_business_table_name": "",
"type": "",
"fkey_business_column_name": '"ofline_online_test"."ORDER_ITEMS"."PRODUCT_ID"',
"pkey_business_column_name": '"ofline_online_test"."PRODUCTS"."PRODUCT_ID"',
"from_type": "0..1",
"to_type": "n..n",
},
},
{
"name": "",
"id": "",
"type": "",
"relationship": {
"id": "3006:35",
"fkey_logical_column_id": "2006:67",
"pkey_logical_column_id": "2006:61",
"fkey_column_id": "3003:26",
"fkey_column_name": '"localhost:1521/orcl".."OT"."PRODUCTS"."CATEGORY_ID"',
"pkey_column_id": "3003:22",
"pkey_column_name": '"localhost:1521/orcl".."OT"."PRODUCT_CATEGORIES"."CATEGORY_ID"',
"fkey_table_id": "3001:25",
"pkey_table_id": "3001:21",
"pkey_physical_table_name": "",
"pkey_business_table_name": "",
"type": "",
"fkey_business_column_name": '"ofline_online_test"."PRODUCTS"."CATEGORY_ID"',
"pkey_business_column_name": '"ofline_online_test"."PRODUCT_CATEGORIES"."CATEGORY_ID"',
"from_type": "0..1",
"to_type": "n..n",
},
},
]
}
這是對您自己的代碼稍作修改的答案:
id_input = "3001:14"
item['relationship'] = list(filter(lambda i: i['relationship']['pkey_table_id'] != id_input, item['relationship']))
id_input
是一個有效的字符串!這應該工作
val_to_remove = '3001:25'
for i in item['relationship']:
if i['relationship']['pkey_table_id'] == val_to_remove :
item['relationship'].remove(i)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.