2017-02-14 45 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] 
相關問題