我有以下字典列表:
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 ** 我想从列表中删除该特定项目。所以我的结果是 需要的输出
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']))
答案 0 :(得分:2)
以下应该有效:
id_input = '3001:14'
item["relationship"] = [
i for i in item["relationship"] if i["relationship"]["pkey_table_id"] != id_input
]
答案 1 :(得分:1)
你几乎是对的,稍微更正了你的代码:
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",
},
},
]
}
答案 2 :(得分:0)
这是对您自己的代码稍加修改后的答案:
id_input = "3001:14"
item['relationship'] = list(filter(lambda i: i['relationship']['pkey_table_id'] != id_input, item['relationship']))
id_input
是有效字符串!答案 3 :(得分:0)
这应该有效
val_to_remove = '3001:25'
for i in item['relationship']:
if i['relationship']['pkey_table_id'] == val_to_remove :
item['relationship'].remove(i)