Python 如何从嵌套字典中删除特定值?

Python 如何从嵌套字典中删除特定值?,python,dictionary,Python,Dictionary,我有一个嵌套字典,dict1,如下所示: {'pic1': {'filename': 'pic1.png', 'size': 545, 'regions': [{'shape_attributes': {'name': 'polygon', 'x_values': [211, 205, 214, 232, 254, 263, 265, 265, 263, 257, 221], 'y_values': [186, 200, 214, 21

我有一个嵌套字典,dict1,如下所示:

      {'pic1': {'filename': 'pic1.png',
      'size': 545,
      'regions': [{'shape_attributes': {'name': 'polygon',
      'x_values': [211, 205, 214, 232, 254, 263, 265, 265, 263, 257, 221],
      'y_values': [186, 200, 214, 218, 214, 204, 198, 190, 187, 181, 180]},
      'type': {'animal': '1'}},
      {'shape_attributes': {'name': 'polygon',
      'x_values': [272, 266, 275, 293, 315, 324, 326, 326, 324, 318, 282],
      'y_values': [233, 247, 261, 265, 261, 251, 245, 237, 234, 228, 227]},
      'type': {'animal': '2'}},
      {'shape_attributes': {'name': 'polygon',
      'x_values': [366, 360, 369, 387, 409, 418, 420, 420, 418, 412, 376],
      'y_values': [315, 329, 343, 347, 343, 333, 327, 319, 316, 310, 309]},
      'type': {'animal': '2'}},
      {'shape_attributes': {'name': 'polygon',
      'x_values': [201, 195, 204, 222, 244, 253, 255, 255, 253, 247, 211],
      'y_values': [224, 238, 252, 256, 252, 242, 236, 228, 225, 219, 218]},
      'type': {'animal': '3'}}],
      'file_attributes': {}},
      'pic2': {'filename': 'pic2.png',
      'size': 456,
      'regions': [{'shape_attributes': {'name': 'polygon',
      'x_values': [211, 205, 214, 232, 254, 263, 265, 265, 263, 257, 221],
      'y_values': [186, 200, 214, 218, 214, 204, 198, 190, 187, 181, 180]},
      'type': {'animal': '1'}},
      {'shape_attributes': {'name': 'polygon',
      'x_values': [272, 266, 275, 293, 315, 324, 326, 326, 324, 318, 282],
      'y_values': [233, 247, 261, 265, 261, 251, 245, 237, 234, 228, 227]},
      'type': {'animal': '2'}},
      {'shape_attributes': {'name': 'polygon',
      'x_values': [366, 360, 369, 387, 409, 418, 420, 420, 418, 412, 376],
      'y_values': [315, 329, 343, 347, 343, 333, 327, 319, 316, 310, 309]},
      'type': {'animal': '2'}},
      {'shape_attributes': {'name': 'polygon',
      'x_values': [201, 195, 204, 222, 244, 253, 255, 255, 253, 247, 211],
      'y_values': [224, 238, 252, 256, 252, 242, 236, 228, 225, 219, 218]},
      'type': {'animal': '3'}}],
      'file_attributes': {}}}
我想检查x_值或y_值是否大于500。如果大于500,我必须将其从字典中删除。例如

    {'shape_attributes': {'name': 'polygon',
    'x_values': [366, 360, 369, 387, 409, 418, 420, 420, 418, 412, 376],
    'y_values': [315, 329, 343, 347, 343, 333, 327, 319, 316, 310, 309]},
    'type': {'animal': '2'}}`
如果该键值对满足上述条件,则应从字典中删除所有键值对

有人能帮我吗!谢谢

这将起作用:

for key in dict1.keys():
    dict1[key]['regions'] = [value for value in dict1[key]['regions'] if
                               (max(value['shape_attributes']['x_values'])<=500)
                               and (max(value['shape_attributes']['y_values'])<=500)]
dict1.keys()中的键的
:
dict1[key]['regions']=[dict1[key]['regions']中的值的值,如果

(最大值['shape\u attributes']['x\u values'])此解决方案递归地向下移动传递的对象。当对象是列表时,它递归地向下移动每个列表项,如果报告返回要删除的搜索项已找到,则将其从列表中删除。当对象是字典时,将测试它是否具有键“shape_attributes”。如果是,然后做进一步的测试,看看这个字典是否应该从它的父列表中删除,它应该,True将返回。否则,我们递归地降低字典的值

from itertools import chain

def search_and_remove(o):
    if isinstance(o, list):
        for i, x in enumerate(o):
            if search_and_remove(x):
                del o[i]
        return False
    elif isinstance(o, dict):
        if 'shape_attributes' in o:
            d = o['shape_attributes']
            return any(map(lambda n: n > 500, chain(d['x_values'], d['y_values'])))
        else:
            for v in o.values():
                search_and_remove(v)
            return False

d =   {'pic1': {'filename': 'pic1.png',
      'size': 545,
      'regions': [{'shape_attributes': {'name': 'polygon',
      'x_values': [211, 205, 214, 232, 254, 263, 265, 265, 263, 257, 221],
      'y_values': [186, 200, 214, 218, 214, 204, 198, 190, 187, 181, 180]},
      'type': {'animal': '1'}},
      {'shape_attributes': {'name': 'polygon',
      'x_values': [272, 266, 275, 293, 315, 324, 326, 326, 324, 318, 282],
      'y_values': [233, 247, 261, 265, 261, 251, 245, 237, 234, 228, 227]},
      'type': {'animal': '2'}},
      {'shape_attributes': {'name': 'polygon',
      'x_values': [366, 360, 369, 387, 409, 418, 420, 420, 418, 412, 376],
      'y_values': [315, 329, 343, 347, 343, 333, 327, 319, 316, 310, 309]},
      'type': {'animal': '2'}},
      {'shape_attributes': {'name': 'polygon',
      'x_values': [201, 195, 204, 222, 244, 253, 255, 255, 253, 247, 211],
      'y_values': [224, 238, 252, 256, 252, 242, 236, 228, 225, 219, 218]},
      'type': {'animal': '3'}}],
      'file_attributes': {}},
      'pic2': {'filename': 'pic2.png',
      'size': 456,
      'regions': [{'shape_attributes': {'name': 'polygon',
      'x_values': [211, 205, 214, 232, 254, 263, 265, 265, 263, 257, 221],
      'y_values': [186, 200, 214, 218, 214, 204, 198, 190, 187, 181, 180]},
      'type': {'animal': '1'}},
      {'shape_attributes': {'name': 'polygon',
      'x_values': [272, 266, 275, 293, 315, 324, 326, 326, 324, 318, 282],
      'y_values': [233, 247, 261, 265, 261, 251, 245, 237, 234, 228, 227]},
      'type': {'animal': '2'}},
      {'shape_attributes': {'name': 'polygon',
      'x_values': [366, 360, 369, 387, 409, 418, 420, 420, 418, 412, 376],
      'y_values': [315, 329, 343, 347, 343, 333, 327, 319, 316, 310, 309]},
      'type': {'animal': '2'}},
      {'shape_attributes': {'name': 'polygon',
      'x_values': [201, 195, 204, 222, 244, 253, 255, 255, 253, 247, 211],
      'y_values': [224, 238, 252, 256, 252, 242, 236, 228, 225, 219, 218]},
      'type': {'animal': '3'}}],
      'file_attributes': {}}}

search_and_remove(d)
print(d)
您可以使用递归:

data = {'pic1': {'filename': 'pic1.png', 'size': 545, 'regions': [{'shape_attributes': {'name': 'polygon', 'x_values': [211, 205, 214, 232, 254, 263, 265, 265, 263, 257, 221], 'y_values': [186, 200, 214, 218, 214, 204, 198, 190, 187, 181, 180]}, 'type': {'animal': '1'}}, {'shape_attributes': {'name': 'polygon', 'x_values': [272, 266, 275, 293, 315, 324, 326, 326, 324, 318, 282], 'y_values': [233, 247, 261, 265, 261, 251, 245, 237, 234, 228, 227]}, 'type': {'animal': '2'}}, {'shape_attributes': {'name': 'polygon', 'x_values': [366, 360, 369, 387, 409, 418, 420, 420, 418, 412, 376], 'y_values': [315, 329, 343, 347, 343, 333, 327, 319, 316, 310, 309]}, 'type': {'animal': '2'}}, {'shape_attributes': {'name': 'polygon', 'x_values': [201, 195, 204, 222, 244, 253, 255, 255, 253, 247, 211], 'y_values': [224, 238, 252, 256, 252, 242, 236, 228, 225, 219, 218]}, 'type': {'animal': '3'}}], 'file_attributes': {}}, 'pic2': {'filename': 'pic2.png', 'size': 456, 'regions': [{'shape_attributes': {'name': 'polygon', 'x_values': [211, 205, 214, 232, 254, 263, 265, 265, 263, 257, 221], 'y_values': [186, 200, 214, 218, 214, 204, 198, 190, 187, 181, 180]}, 'type': {'animal': '1'}}, {'shape_attributes': {'name': 'polygon', 'x_values': [272, 266, 275, 293, 315, 324, 326, 326, 324, 318, 282], 'y_values': [233, 247, 261, 265, 261, 251, 245, 237, 234, 228, 227]}, 'type': {'animal': '2'}}, {'shape_attributes': {'name': 'polygon', 'x_values': [366, 360, 369, 387, 409, 418, 420, 420, 418, 412, 376], 'y_values': [315, 329, 343, 347, 343, 333, 327, 319, 316, 310, 309]}, 'type': {'animal': '2'}}, {'shape_attributes': {'name': 'polygon', 'x_values': [201, 195, 204, 222, 244, 253, 255, 255, 253, 247, 211], 'y_values': [224, 238, 252, 256, 252, 242, 236, 228, 225, 219, 218]}, 'type': {'animal': '3'}}], 'file_attributes': {}}}
def valid(v):
  return not isinstance(v, list) or all(i < 500 for i in v if not isinstance(i, (dict, list)))

def r_dict(d):
  if isinstance(d, list):
     return [r_dict(i) if isinstance(i, dict) else i for i in d]
  return {a:r_dict(b) if isinstance(b, dict) else b for a, b in d.items() if valid(b)}

result = r_dict(data)

从代码可读性的角度来看,使用按位
运算符而不是逻辑
?的任何原因似乎都更有意义(因为它们实际上是逻辑值),并提供短路评估,这在这里是一个优点。使用
没有具体原因,代码类型问题Isuppose@user34:对我来说很好。请确保按照上面编辑的方式启动
orig_dict
(您在那里有语法错误)。那么你就没事了。我认为你的答案值得更改,因为这似乎是一个新的Python用户,这是一个坏习惯。除了我给出的两个原因外,它对于真实和虚假的值也不能正常工作,例如
True和[3,4]
True
但是
True&[3,4]
引发
类型错误
异常。
{'pic1': {'filename': 'pic1.png', 'size': 545, 'regions': [{'shape_attributes': {'name': 'polygon', 'x_values': [211, 205, 214, 232, 254, 263, 265, 265, 263, 257, 221], 'y_values': [186, 200, 214, 218, 214, 204, 198, 190, 187, 181, 180]}, 'type': {'animal': '1'}}, {'shape_attributes': {'name': 'polygon', 'x_values': [272, 266, 275, 293, 315, 324, 326, 326, 324, 318, 282], 'y_values': [233, 247, 261, 265, 261, 251, 245, 237, 234, 228, 227]}, 'type': {'animal': '2'}}, {'shape_attributes': {'name': 'polygon', 'x_values': [366, 360, 369, 387, 409, 418, 420, 420, 418, 412, 376], 'y_values': [315, 329, 343, 347, 343, 333, 327, 319, 316, 310, 309]}, 'type': {'animal': '2'}}, {'shape_attributes': {'name': 'polygon', 'x_values': [201, 195, 204, 222, 244, 253, 255, 255, 253, 247, 211], 'y_values': [224, 238, 252, 256, 252, 242, 236, 228, 225, 219, 218]}, 'type': {'animal': '3'}}], 'file_attributes': {}}, 'pic2': {'filename': 'pic2.png', 'size': 456, 'regions': [{'shape_attributes': {'name': 'polygon', 'x_values': [211, 205, 214, 232, 254, 263, 265, 265, 263, 257, 221], 'y_values': [186, 200, 214, 218, 214, 204, 198, 190, 187, 181, 180]}, 'type': {'animal': '1'}}, {'shape_attributes': {'name': 'polygon', 'x_values': [272, 266, 275, 293, 315, 324, 326, 326, 324, 318, 282], 'y_values': [233, 247, 261, 265, 261, 251, 245, 237, 234, 228, 227]}, 'type': {'animal': '2'}}, {'shape_attributes': {'name': 'polygon', 'x_values': [366, 360, 369, 387, 409, 418, 420, 420, 418, 412, 376], 'y_values': [315, 329, 343, 347, 343, 333, 327, 319, 316, 310, 309]}, 'type': {'animal': '2'}}, {'shape_attributes': {'name': 'polygon', 'x_values': [201, 195, 204, 222, 244, 253, 255, 255, 253, 247, 211], 'y_values': [224, 238, 252, 256, 252, 242, 236, 228, 225, 219, 218]}, 'type': {'animal': '3'}}], 'file_attributes': {}}}