从字典中删除元数据基于Python的布尔值

时间:2017-11-01 14:10:24

标签: python dictionary

我想根据分配从列表中删除json数据。如果分配:True 那么,那个特定的josn应该从字典中删除。 在下面的数据中 {" paperWidth":" 4","分配":正确,"型号":" RB3150"," connType":[" Lan"," Wifi"," BlueTooth"]," commandSet&#34 ;:" TVSPOS"}

所以这应该从我的最终对象中删除。如何在python中执行此操作。

d = {"allPrinters": [{"models": [{"paperWidth": "4","allocation": True,"model": "RB3150","connType": ["Lan","Wifi","BlueTooth"],"commandSet": "TVSPOS"},{"paperWidth": "4","allocation": False,"model": "RB3151","connType": ["Lan","Wifi"],"commandSet": "TVSPOS"}],"active": True,"make": "TVS","_id": "59e6f670f532eb1260b50cd9"},{"models": [{"paperWidth": "4","allocation": False,"model": "EP3160","connType": ["Lan","Wifi"],"commandSet": "ESCPOS"},{"paperWidth": "4","allocation": True,"model": "EP3161","connType": ["Lan"],"commandSet": "ESCPOS"}],"active": True,"make": "EPSON","_id": "59e6fc8ff532eb1260b50cf4"}]}

谢谢你, Ramesh M

1 个答案:

答案 0 :(得分:1)

我不确定你要删除的东西是整个打印机还是只是模型,但这应该适用于前一种情况并且很容易转换为后者(转储continue并替换d['allPrinters']model}:

for i, printer in enumerate(d['allPrinters']):
     for model in printer['models']:
         if model['allocation']:
             d['allPrinters'].pop(i)
             continue