如何从json dict中删除匹配的列与python中的列表进行比较

时间:2017-02-14 07:36:17

标签: python json dictionary

我有一个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"}]

2 个答案:

答案 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