Python 无法使用del方法从目录列表中删除词典
我试图删除每一个点值为0的字典,但当我运行此代码时,对象仍然保留。这里有没有一个特例,为什么它不会删除Python 无法使用del方法从目录列表中删除词典,python,dictionary,Python,Dictionary,我试图删除每一个点值为0的字典,但当我运行此代码时,对象仍然保留。这里有没有一个特例,为什么它不会删除 import json import numpy as np import pandas as pd from itertools import groupby # using json open the player objects file and set it equal to data with open('Combined_Players_DK.json') as json_fil
import json
import numpy as np
import pandas as pd
from itertools import groupby
# using json open the player objects file and set it equal to data
with open('Combined_Players_DK.json') as json_file:
player_data = json.load(json_file)
for player in player_data:
for points in player['Tournaments']:
player['Average'] = round(sum(float(tourny['Points']) for tourny in player['Tournaments']) / len(player['Tournaments']),2)
for players in player_data:
for value in players['Tournaments']:
if value['Points'] == 0:
del value
with open('PGA_Player_Objects_With_Average.json', 'w') as my_file:
json.dump(player_data, my_file)
这里是JSON
[
{
"Name": "Dustin Johnson",
"Tournaments": [
{
"Date": "2020-06-25",
"Points": 133.5
},
{
"Date": "2020-06-18",
"Points": 101
},
{
"Date": "2020-06-11",
"Points": 25
},
{
"Date": "2020-02-20",
"Points": 60
},
{
"Date": "2020-02-13",
"Points": 89.5
},
{
"Date": "2020-02-06",
"Points": 69.5
},
{
"Date": "2020-01-02",
"Points": 91
},
{
"Date": "2019-12-04",
"Points": 0
}
],
"Average": 71.19
}]
我不确定为什么不能使用delete值。我也尝试过删除,但留下了一个空对象。循环时不能使用del删除值,正如您所见,如果您想使用del,您应该根据其索引而不是for循环范围内的值来删除该项,因为@MosesKoledoye在评论中说:
您希望删除del players['tournames']以对
dictionary&删除该条目,而不是仅删除的del值
范围中的值名称。参见stmt
当您循环并想要修改列表时,您必须创建一个副本,如图中所示。我建议您查看上面的内容,查看在循环时删除元素的其他方法。试试这个:
for players in player_data:
for i in range(len(players['Tournaments'])):
if players['Tournaments'][i]['Points'] == 0:
del players['Tournaments'][i]
我更喜欢使用更好的词典理解,因此您也可以尝试以下方法:
player_data[0]['Tournaments']=[dct for dct in player_data[0]['Tournaments'] if dct['Points']!=0]
print(data)
输出:
[{'Name': 'Dustin Johnson', 'Tournaments': [{'Date': '2020-06-25', 'Points': 133.5}, {'Date': '2020-06-18', 'Points': 101}, {'Date': '2020-06-11', 'Points': 25}, {'Date': '2020-02-20', 'Points': 60}, {'Date': '2020-02-13', 'Points': 89.5}, {'Date': '2020-02-06', 'Points': 69.5}, {'Date': '2020-01-02', 'Points': 91}], 'Average': 71.19}]
循环时不能使用del删除值,如您所见,如果要使用del,则应根据其索引而不是for循环范围内的值来删除该项,因为@MosesKoledoye在评论中说:
您希望删除del players['tournames']以对
dictionary&删除该条目,而不是仅删除的del值
范围中的值名称。参见stmt
当您循环并想要修改列表时,您必须创建一个副本,如图中所示。我建议您查看上面的内容,查看在循环时删除元素的其他方法。试试这个:
for players in player_data:
for i in range(len(players['Tournaments'])):
if players['Tournaments'][i]['Points'] == 0:
del players['Tournaments'][i]
我更喜欢使用更好的词典理解,因此您也可以尝试以下方法:
player_data[0]['Tournaments']=[dct for dct in player_data[0]['Tournaments'] if dct['Points']!=0]
print(data)
输出:
[{'Name': 'Dustin Johnson', 'Tournaments': [{'Date': '2020-06-25', 'Points': 133.5}, {'Date': '2020-06-18', 'Points': 101}, {'Date': '2020-06-11', 'Points': 25}, {'Date': '2020-02-20', 'Points': 60}, {'Date': '2020-02-13', 'Points': 89.5}, {'Date': '2020-02-06', 'Points': 69.5}, {'Date': '2020-01-02', 'Points': 91}], 'Average': 71.19}]
即使当我printvalue['Points']==0时,它也会返回trueYou want del players['Tournaments'],以便对字典执行删除操作&删除该条目,而不是删除仅从范围中删除值名的del value。请参阅。即使当我printvalue['Points']==0时,它也会返回trueYou want del players['tournames',以便对字典执行删除操作&删除该条目,而不是仅从范围中删除值名的del value。看,真是太棒了。你知道为什么我这样做不管用吗?有什么特殊情况吗something@AustinJohnson当您删除值时,对dictionary的引用会被删除,但dictionary对象不会被删除,因为列表中仍然包含对dictionary对象的引用。@AustinJohnson只是对方法不起作用的原因做了一点解释。希望对您有所帮助。此外,你可以考虑接受答案:那真是太棒了。你知道为什么我这样做不管用吗?有什么特殊情况吗something@AustinJohnson当您删除值时,对dictionary的引用会被删除,但dictionary对象不会被删除,因为列表中仍然包含对dictionary对象的引用。@AustinJohnson只是对方法不起作用的原因做了一点解释。希望对您有所帮助。此外,你可以考虑接受答案: