我有以下字典列表:
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"][" relationship"]["pkey_table_id"]的第一项。我想循环通过列表键项["关系"],如果匹配被发现与**pkey_table_id = 3001:14 **我想从列表中删除该特定的项目。所以我的结果将是要求输出
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)
输出:
{
"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",
},
},
]
}
应该可以了
val_to_remove = '3001:25'
for i in item['relationship']:
if i['relationship']['pkey_table_id'] == val_to_remove :
item['relationship'].remove(i)
只需对自己的代码稍加修改,就可以得到答案:
id_input = "3001:14"
item['relationship'] = list(filter(lambda i: i['relationship']['pkey_table_id'] != id_input, item['relationship']))
- 确保
id_input
是一个有效的字符串! - 必须将item['relationship']传递给过滤函数。