![](/img/trans.png)
[英]Python how to delete a specific value from a dictionary key with multiple values?
[英]How to delete specific values from a nested 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, 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_values 或 y_values 大于 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)]
由于您当前没有大于 500 的值,因此不会整理任何内容。
此解决方案递归地向下传递传递的 object。 当 object 是一个列表时,它递归地向下遍历每个列表项,如果返回报告已找到要删除的搜索项,则将其从列表中删除。 当 object 是字典时,测试它是否具有键 '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)
Output:
{'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': {}}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.