我有一个json列表和一个json dict,我想从dict中删除'id'列与列表元素匹配的对象。
a = [1,2,3,4,5]
b = {"data":[{"id":1,"name":"shubham"},{"id":8,"name":"rahul"}]
我希望输出如下:
b = {"data":[{"id":8,"name":"rahul"}]
答案 0 :(得分: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]
答案 1 :(得分: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