Python dictionary: pop or del



我看不懂python字典

假设我有一个名为data的字典它看起来像这样:

{ "computers": [
{"Netbios_Name0": "apple1", "User_Domain0": "paradise", "User_Name0": "adam", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9058.1018"}, 
{"Netbios_Name0": "apple2", "User_Domain0": "paradise", "User_Name0": "lilith", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9040.1044"}, 
{"Netbios_Name0": "apple3", "User_Domain0": "paradise", "User_Name0": "eve", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9068.1026"}
]}

,我想要删除apple2,所以我的最终结果看起来像这样:

{ "computers": [
{"Netbios_Name0": "apple1", "User_Domain0": "paradise", "User_Name0": "adam", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9058.1018"},  
{"Netbios_Name0": "apple3", "User_Domain0": "paradise", "User_Name0": "eve", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9068.1026"}
]}

我想我必须这样做:

for item in data['computers']:
if "apple2" in item['Netbios_Name0']:
item.pop() # or del data[item]

但是我不能让它工作。

您可以使用remove():

data = { "computers": [
{"Netbios_Name0": "apple1", "User_Domain0": "paradise", "User_Name0": "adam", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9058.1018"}, 
{"Netbios_Name0": "apple2", "User_Domain0": "paradise", "User_Name0": "lilith", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9040.1044"}, 
{"Netbios_Name0": "apple3", "User_Domain0": "paradise", "User_Name0": "eve", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9068.1026"}
]}
for item in list(data['computers']):
if 'apple2' in item['Netbios_Name0']:
data['computers'].remove(item)
data

输出:

{'computers': [{'Netbios_Name0': 'apple1',
'User_Domain0': 'paradise',
'User_Name0': 'adam',
'SMS_Installed_Sites0': 'heaven',
'Client_Version0': '5.00.9058.1018'},
{'Netbios_Name0': 'apple3',
'User_Domain0': 'paradise',
'User_Name0': 'eve',
'SMS_Installed_Sites0': 'heaven',
'Client_Version0': '5.00.9068.1026'}]}

你正在迭代一个列表——你可以通过索引从列表中弹出。

永远不要在迭代时修改列表的长度:如何在迭代时从列表中删除项?

一种方法是先存储要删除的索引,然后再删除它们:

data = { "computers": [
{"Netbios_Name0": "apple1", "User_Domain0": "paradise", "User_Name0": "adam", 
"SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9058.1018"}, 
{"Netbios_Name0": "apple2", "User_Domain0": "paradise", "User_Name0": "lilith", 
"SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9040.1044"}, 
{"Netbios_Name0": "apple3", "User_Domain0": "paradise", "User_Name0": "eve", 
"SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9068.1026"}
]}
to_delete = []
for idx, inner_dic in enumerate(data['computers']):
if "apple2" in inner_dic['Netbios_Name0']:
to_delete.append(idx)
# remove biggest to lowest indexes - removing does not influence the order 
for idx in to_delete[::-1]:
data['computers'].pop(idx)
print(data)

输出:

{'computers': 
[{'Netbios_Name0': 'apple1', 'User_Domain0': 'paradise', 'User_Name0': 'adam', 
'SMS_Installed_Sites0': 'heaven', 'Client_Version0': '5.00.9058.1018'}, 
{'Netbios_Name0': 'apple3', 'User_Domain0': 'paradise', 'User_Name0': 'eve', 
'SMS_Installed_Sites0': 'heaven', 'Client_Version0': '5.00.9068.1026'}]
}

Python的内置enumerate函数允许我们遍历列表并检索列表中每个项的索引和值:

data = { "computers": [
{"Netbios_Name0": "apple1", "User_Domain0": "paradise", "User_Name0": "adam", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9058.1018"}, 
{"Netbios_Name0": "apple2", "User_Domain0": "paradise", "User_Name0": "lilith", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9040.1044"}, 
{"Netbios_Name0": "apple3", "User_Domain0": "paradise", "User_Name0": "eve", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9068.1026"}
]}
# Here 0 in 2nd argument is the starting number for num variable, can be used 0 to indicate as index
for num, computer in enumerate(data["computers"], 0):
if computer["Netbios_Name0"] == "apple2":
del data["computers"][num]
print(data)

应该可以。

您可以在这里找到更多信息:https://realpython.com/iterate-through-dictionary-python/

相关内容

  • 没有找到相关文章

最新更新