在Python中从列表中删除字典的问题

时间:2015-07-26 23:07:29

标签: python json dictionary

我的代码中有几条逻辑,如果符合条件,我会删除键/值或字典。这些对象在列表中时会被删除,但是一旦它们被附加到顶级字典并变成JSON对象,它们就会重新出现,从而否定了我的逻辑。可能是什么原因造成的?

这是我的代码示例,显示了字典如何附加逻辑删除键/值和字典;

nil

使用上面的代码输出示例;

dL311 = dict()
d = dict()
d.setdefault("DriverFirstName",last_edited_user )
d.setdefault("DriverLastName","Aguilar" )
d.setdefault("LastUpdatedBy", "SANSTAR1")
d.setdefault("DACType", dar_item_1)
d.setdefault("Type", "Dead Animal Removal")
d.setdefault("Name", dar_uid_1 )
d.setdefault("DACItemCount", dar_qyt_1)
l311.append(d)
dL311 = dict()
d = dict()
d.setdefault("DriverFirstName",last_edited_user )
d.setdefault("DriverLastName","Aguilar" )
d.setdefault("LastUpdatedBy", "SANSTAR1")
d.setdefault("DACType", dar_item_2)
d.setdefault("Type", "Dead Animal Removal")
d.setdefault("Name", dar_uid_2)
d.setdefault("DACItemCount", dar_qyt_2)
l311.append(d)
d = dict()
d.setdefault("DriverFirstName",last_edited_user )
d.setdefault("DriverLastName","Aguilar" )
d.setdefault("LastUpdatedBy", "SANSTAR1")
d.setdefault("DACType", dar_item_3)
d.setdefault("Type", "Dead Animal Removal")
d.setdefault("Name", dar_uid_3)
d.setdefault("DACItemCount", dar_qyt_3)
l311.append(d)
d = dict()
d.setdefault("DriverFirstName",last_edited_user )
d.setdefault("DriverLastName","Aguilar" )
d.setdefault("LastUpdatedBy", "SANSTAR1")
d.setdefault("DACType", dar_item_4)
d.setdefault("Type", "Dead Animal Removal")
d.setdefault("Name", dar_uid_4)
d.setdefault("DACItemCount", dar_qyt_4)
l311.append(d)
d = dict()
d.setdefault("DriverFirstName",last_edited_user )
d.setdefault("DriverLastName","Aguilar" )
d.setdefault("LastUpdatedBy", "SANSTAR1")
d.setdefault("DACType", dar_item_5)
d.setdefault("Type", "Dead Animal Removal")
d.setdefault("Name", dar_uid_5)
d.setdefault("DACItemCount", dar_qyt_5)
l311.append(d)
d = dict()
d.setdefault("DriverFirstName",last_edited_user )
d.setdefault("DriverLastName","Aguilar" )
d.setdefault("LastUpdatedBy", "SANSTAR1")
d.setdefault("DACType", dar_item_6)
d.setdefault("Type", "Dead Animal Removal")
d.setdefault("Name", dar_uid_6)
d.setdefault("DACItemCount", dar_qyt_6)
l311.append(d)
d = dict()
d.setdefault("DriverFirstName",last_edited_user )
d.setdefault("DriverLastName","Aguilar" )
d.setdefault("LastUpdatedBy", "SANSTAR1")
d.setdefault("DACType", dar_item_7)
d.setdefault("Type", "Dead Animal Removal")
d.setdefault("Name", dar_uid_7)
d.setdefault("DACItemCount", dar_qyt_7)
l311.append(d)
d = dict()
d.setdefault("DriverFirstName",last_edited_user )
d.setdefault("DriverLastName","Aguilar" )
d.setdefault("LastUpdatedBy", "SANSTAR1")
d.setdefault("DACType", dar_item_8)
d.setdefault("Type", "Dead Animal Removal")
d.setdefault("Name", dar_uid_8)
d.setdefault("DACItemCount", dar_qyt_8)
l311.append(d)
d = dict()
d.setdefault("DriverFirstName",last_edited_user )
d.setdefault("DriverLastName","Aguilar" )
d.setdefault("LastUpdatedBy", "SANSTAR1")
d.setdefault("DACType", dar_item_9)
d.setdefault("Type", "Dead Animal Removal")
d.setdefault("Name", dar_uid_9)
d.setdefault("DACItemCount", dar_qyt_9)
l311.append(d)
d = dict()
d.setdefault("DriverFirstName",last_edited_user )
d.setdefault("DriverLastName","Aguilar" )
d.setdefault("LastUpdatedBy", "SANSTAR1")
d.setdefault("DACType", dar_item_10)
d.setdefault("Type", "Dead Animal Removal")
d.setdefault("Name", dar_uid_10)
d.setdefault("DACItemCount", dar_qyt_10)
l311.append(d)

for d in l311:
    if(d['Name'].strip() == '') and((type(d['DACItemCount']) != int) or (d['DACItemCount']==0)):
        del d['Name']
        del d['DACItemCount']

lIndexes = []
nCnt = len(l311)
for i in range(nCnt):
    dd = l311[i]
    if(dd==d):
        lIndexes.append(i)


for i in lIndexes:
    i

#remove the d from the original l311 in a reverse order:
# print ("order (of index) in which items are deleted:" )
for ii in reversed(lIndexes):
    if  d.setdefault("Name", dar_uid_10) == ' ':
        # print(ii)
        del l311[ii]


print  (l311)


dL311 = dict()
dL311.setdefault("DeadAnimalRemoval", l311)
dResult.setdefault("ListOfLa311DeadAnimalRemoval",dL311)

#Ends of adding additional itmes ****************************************
lResults.append({"MetaData": {}, "SRData": dResult})

没有删除词典的JSON输出;

[{'Name': '071920150633557841', 'DACType': ' ', 'DriverLastName': 'Aguilar', 'DriverFirstName': 'SA', 'LastUpdatedBy': 'SANSTAR1', 'DACItemCount': '0', 'Type': 'Dead Animal Removal'}]

我希望删除第二个字典,因为没有'Name'值。

1 个答案:

答案 0 :(得分:1)

如果删除名称,则if d.setdefault("Name", dar_uid_10) == ' '将添加名称 - 作为dar_uid_10。除非dar_uid_10 =='',否则此代码永远不会认为字典的名称是空格。

我认为只需使用if 'Name' not in d即可。