简体   繁体   English

从python中的多值字典中删除特定值

[英]Delete a specific value from multi-value dictionary in python

``I have a dictionary in python like this. ``我在python中有这样的字典。

dictionary = {"00":[1,2,3,4,5,6,7,8,9],"01":[1,2,3,4,5,6,7,8,9],"02":[1,2,3,4,5,6,7,8,9],"03":[1,2,3,4,5,6,7,8,9],"04":[1,2,3,4,5,6,7,8,9]........up-to "99":[1,2,3,4,5,6,7,8,9]}

I have to delete the value 2 from the list of "00".I tried it using following statement. 我必须从“ 00”列表中删除值2。我使用以下语句尝试了它。

 del (dictionary[key][dictionary[key].index(sudokumatrix[i][iindex])]).

Here key has value "00" and sudokumatrix[i][iindex] has value 2.But i got resulting dictionary as 在这里,键的值为“ 00”,sudokumatrix [i] [iindex]的值为2。

{"00":[1,3,4,5,6,7,8,9],"01":[1,3,4,5,6,7,8,9],"02":[1,3,4,5,6,7,8,9],"03":[1,3,4,5,6,7,8,9],"04":[1,3,4,5,6,7,8,9].....}.

I have to get the result as: 我必须得到的结果是:

{"00":[1,3,4,5,6,7,8,9],"01":[1,2,3,4,5,6,7,8,9],"02":[1,2,3,4,5,6,7,8,9],"03":[1,2,3,4,5,6,7,8,9],"04":[1,2,3,4,5,6,7,8,9]....}

I am posting the whole code here: 我将整个代码发布在这里:

dictionary = dict()
zerotonine = "123456789"
list2 = list(zerotonine)
list2 = [int(i) for i in list2]
sudokumatrix=[]
for p in range(9):
     for q in range(9):
           keyis=str(p)+str(q)
           dictionary[keyis] = list2 


for i in range(9):
     initialinput = [1,2,3,4,5,6,7,8,9]
     list1=list(initialinput)
     list1 = [int(i) for i in list1]
     sudokumatrix.append(list1)

key = "00"

del dictionary[key][dictionary[key].index(sudokumatrix[0][1])]


print dictionary

EDIT == I guess(since the generation of dictionary is not given) ==EDIT . 编辑==我猜(因为没有给出dictionary的生成) == EDIT The reason is that the values of keys '00' , '01' , ... are pointing to the same list. 原因是键'00''01' ,...的值指向同一列表。 Modifying one of them will definitely affect the others. 修改其中之一肯定会影响其他。

Try using this to generate your dict 尝试使用它来生成您的字典

dictionary = dict((str(x).zfill(2), range(1, 10)) for x in range(100))

Your code of this part is actually not wrong, but to use list.remove() will make it much better. 您的这一部分代码实际上没有错,但是使用list.remove()会使它更好。

The issue has to do with pointers. 这个问题与指针有关。

replace this: 替换为:

dictionary[keyis] = list2 

with this: 有了这个:

dictionary[keyis] = [int(i) for i in list2]

You're creating list2 correctly, but when you go into the loop Python doesn't make a brand new copy of it with every iteration. 您正在正确创建list2,但是当您进入循环时,Python不会在每次迭代时都为其创建一个全新的副本。 It makes a pointer to the original list. 它使指针指向原始列表。 Python sees: Python看到:

dictionary[keyis] = list2

and says "oh, list2? I recognize that name! I have that as an object in memory already! I'll save some space by just updating the original copy and linking it here! Any time someone wants to view it or update it I'll just deal with the original and everything will be awesome forever!!!" 并说:“哦,list2?我已经知道了这个名字!我已经将它作为对象存储在内存中了!我将通过更新原始副本并将其链接到此处来节省一些空间!任何时候有人想要查看或更新它时,我“只要处理原始图片,一切都会永远很棒!!!”

OK, so maybe the python interpreter isn't that enthusiastic, but that's how I like to think of it. 好的,所以也许python解释器不是那么热情,但这就是我想的方式。 The end result is that all of your dictionary values are pointing at the original list. 最终结果是您所有的字典值都指向原始列表。

If you don't mind deleting every occurrence of 2 in the list, you can use list comprehension: 如果您不介意删除列表中所有出现的2 ,则可以使用列表理解:

dictionary["00"] = [i for i in dictionary["00"] if i != 2]

This will create a new list, and will avoid altering the other values, as it appears all your dictionary values reference the same list. 这将创建一个新列表,并且将避免更改其他值,因为看起来所有dictionary值都引用同一列表。

EDIT: Yep your dictionary values reference the same list 编辑:是的您的字典值引用相同的列表

you could use dictionary and list comprehension to create your dictionary 您可以使用字典和列表理解来创建dictionary

dictionary = {str(x):[i for i in range(10)] for x in range(100)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM