2017-02-14 47 views
-3

我有一个json列表和一个json字典,我想从dict中删除'id'列与列表元素匹配的对象。如何从json中删除匹配的列dict与python中的列表比较

a = [1,2,3,4,5] 
b = {"data":[{"id":1,"name":"shubham"},{"id":8,"name":"rahul"}] 

我想输出,如:

b = {"data":[{"id":8,"name":"rahul"}] 
+0

呀都应该被删除。我的意思是在字典中应该删除与列表a匹配的b的id的所有元素。 – Shubham

+0

那些不是JSON - JSON是文本。这些是Python中的实际列表和字典。 –

回答

0
a = [1,2,3,4,5] 
b = {"data":[{"id":1,"name":"shubham"},{"id":8,"name":"rahul"}] 

items = b['data'] 

for id in a: 
    for it in items: 
     if it['id'] == id: 
     items.remove(it) 
b['data'] = items 
2
a = [1,2,3,4,5] 
b = {"data":[{"id":1,"name":"shubham"},{"id":8,"name":"rahul"}]} 

s = set(a) 
for i, item in enumerate(b['data']): 
    if item['id'] in s: 
     del b['data'][i] 
相关问题