Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Python上的列表中删除字典索引?_Python_List_Dictionary_Pickle - Fatal编程技术网

如何从Python上的列表中删除字典索引?

如何从Python上的列表中删除字典索引?,python,list,dictionary,pickle,Python,List,Dictionary,Pickle,我正在创建一个程序,将玩家的高分存储在拱廊中。 我在这里使用的列表被称为PlayerID,因为它包含一个唯一的ID和其他信息,比如他们在每场比赛中的高分。每当我试图从玩家列表中成功删除字典时,它都无法正常工作,会删除多个配置文件 这是我目前正在使用的代码。Pickle正在用于数据存储 with open("playerdata.dat",'rb') as f: PlayerID = pickle.load(f) while True: try: Searc

我正在创建一个程序,将玩家的高分存储在拱廊中。 我在这里使用的列表被称为PlayerID,因为它包含一个唯一的ID和其他信息,比如他们在每场比赛中的高分。每当我试图从玩家列表中成功删除字典时,它都无法正常工作,会删除多个配置文件

这是我目前正在使用的代码。Pickle正在用于数据存储

with open("playerdata.dat",'rb') as f:
    PlayerID = pickle.load(f)    
while True:
    try:
        SearchID= int(input("Enter the ID of the profile you are removing")) # used to check if a wanted user actually exists in the program
    except ValueError:
        print("You have not provided an integer input, please try again.") #performs type check to ensure a valid input is provided
        continue
    else:
        break    

index= 0
position = -1
for Player in PlayerID:
    if Player['ID'] == SearchID:
        position = index
    else:
        index = index + 1
    try:
        PlayerID.pop(position)
    except IndexError:
        print("The ID provided does not exist.")
print("The user with ID", searchID,", has been deleted")    
with open('playerdata.dat','wb') as f:
    pickle.dump(playerID,f,pickle.HIGHEST_PROTOCOL)

此外,即使输入的整数ID实际上不存在于PlayerID列表中,它仍然会删除多个配置文件,即使我有索引器代码。

这可能是一种更简单的方法:

for index, Player in enumerate(PlayerID):
    if Player['ID'] == SearchID:
        PlayerID.pop(index)
        print("The user with ID", SearchID, ", has been deleted")
        break
else:
    print("The ID provided does not exist.")

这可能是一种更简单的方法:

for index, Player in enumerate(PlayerID):
    if Player['ID'] == SearchID:
        PlayerID.pop(index)
        print("The user with ID", SearchID, ", has been deleted")
        break
else:
    print("The ID provided does not exist.")

问题是
-1
在Python中是一个有效的列表索引;它将弹出列表中的最后一个元素

一旦遇到正确的id,就很容易弹出。此外,您还可以使用
枚举
来计算索引:

for index, player in enumerate(players):
    if player['ID'] == search_id:
        players.pop(index)
        # we expect that the ID is truly unique, there is
        # only 1 occurrence of the ID.
        break
现在,当然有人可能会问,为什么不使用id->player字典来存储玩家-然后您可以执行以下操作:

if search_id in players:
    players.pop(search_id)

问题是
-1
在Python中是一个有效的列表索引;它将弹出列表中的最后一个元素

一旦遇到正确的id,就很容易弹出。此外,您还可以使用
枚举
来计算索引:

for index, player in enumerate(players):
    if player['ID'] == search_id:
        players.pop(index)
        # we expect that the ID is truly unique, there is
        # only 1 occurrence of the ID.
        break
现在,当然有人可能会问,为什么不使用id->player字典来存储玩家-然后您可以执行以下操作:

if search_id in players:
    players.pop(search_id)

我需要使用Index=0的预定义值,还是将索引作为自己的值读取?否,写入的
for
循环将导致
Index
从0开始。您不必自己设置它。我刚刚注意到,在您的代码中,if语句中的“foundID”是否意味着在它的末尾有一个“=True”?不,这不是必需的。
if
语句要求后面有一个布尔值,
foundID
是一个布尔值。如果
foundID
为True,则执行
if
语句的主块,如果
foundID
为False,则执行
else
语句。很抱歉,在此解决方案后出现了另一个错误。代码似乎只想删除字面索引0处的词典。我需要使用预定义的index=0值还是将index读取为其自身的值?否,写入的
for
循环将导致
index
从0开始。您不必自己设置它。我刚刚注意到,在您的代码中,if语句中的“foundID”是否意味着在它的末尾有一个“=True”?不,这不是必需的。
if
语句要求后面有一个布尔值,
foundID
是一个布尔值。如果
foundID
为True,则执行
if
语句的主块,如果
foundID
为False,则执行
else
语句。很抱歉,在此解决方案后出现了另一个错误。代码似乎只想删除索引为0的字典。我尝试了使用您的第一种方法,但它似乎根本无法从playerdata.dat文件中删除所需的字典。在这种情况下,这可能意味着
search\u id
与您文件中的id不匹配;可能您对ID使用了
str
s,但现在输入了一个INT,ID是随机的,通过randint()函数生成。该程序似乎只是拒绝删除包含我要删除的ID的列表。我尝试了使用您的第一种方法,但它似乎根本没有从playerdata.dat文件中删除所需的词典。在这种情况下,它可能意味着
search\u ID
与您文件中的ID不匹配;可能您对ID使用了
str
s,但现在输入了一个INT,ID是随机的,通过randint()函数生成。程序似乎只是拒绝删除包含我要删除的ID的列表