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

时间:2020-04-06 10:14:09

标签: python dictionary

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

    {'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'}}`

我要检查x_values或y_values是否大于500。如果大于500,则必须将其从字典中删除。例如,

@srgerg

如果此键值对满足上述条件,则应从字典中删除所有这些键。

谁能帮助我!谢谢!

3 个答案:

答案 0 :(得分:1)

这将起作用:

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,因此不会进行任何排序。

答案 1 :(得分:1)

此解决方案递归地沿传递的对象下降。当对象是列表时,它递归地下降到每个列表项,并且如果报告返回已找到要删除的搜索项,则将其从列表中删除。当对象是字典时,将对其进行测试以查看其是否具有键'shape_attributes'。如果是这样,则需要进行进一步的测试以查看是否应将此字典从其父列表中删除,并且应该将True返回。否则,我们将递归地递归字典的值。

 batch = next(iter(train_loader))
 images, labels = batch
 grid= torchvision.utils.make_grid(images, nrow=1)

 plt.figure(figsize=(15,15))
 plt.imshow(np.transpose(grid))

答案 2 :(得分:-1)

您可以使用递归:

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)

输出:

{'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': {}}}