简体   繁体   English

从 python 中的字典中删除多个键值对

[英]Deleting multiple key value pairs from dictionary in python

I generated a python dictionary for all the duplicate images in a folder.我为文件夹中的所有重复图像生成了 python 字典。 The python dictonary now contains values in the following format: python 字典现在包含以下格式的值:

{
  "image_1.jpg": ['image_xyz.jpg', 'image_abc.jpg'],
  "image_xyz.jpg": ["image_1.jpg", "image_abc.jpg"],
  "image_abc.jpg": ["image_xyz.jpg","image_1.jpg"],
  "image_2.jpg": ["image_3.jpg"],
  "image_3.jpg": ["image_2.jpg"],
  "image_5.jpg": []
}

Each key, value pair thus appears atleast twice in the list.因此,每个键值对在列表中至少出现两次。 Empty list for keys are present which have no duplicates.存在没有重复项的空键列表。 Is there a way to delete all the duplicate key value pairs present?有没有办法删除所有存在的重复键值对? so that the dictionary looks like the following:使字典如下所示:

{
  "image_1.jpg": ['image_xyz.jpg', 'image_abc.jpg'],
  "image_2.jpg": ["image_3.jpg"],
  "image_5.jpg": []
}

I tried using list to first store all the values from the key value pair and then deleting them from the dictionary but it empties the whole dictionary.我尝试使用 list 首先存储键值对中的所有值,然后从字典中删除它们,但它会清空整个字典。

source = {
  "image_1.jpg": ['image_xyz.jpg', 'image_abc.jpg'],
  "image_xyz.jpg": ["image_1.jpg", "image_abc.jpg"],
  "image_abc.jpg": ["image_xyz.jpg","image_1.jpg"],
  "image_2.jpg": ["image_3.jpg"],
  "image_3.jpg": ["image_2.jpg"],
  "image_5.jpg": []
}

dest = dict()

for k,v in source.items():
    ok = True
    for k1,v1 in dest.items():
        if k in v1: ok = False
    if ok: dest[k] = v

print(dest) # New filtered dict

I usually do this method when getting rid of duplicates in a list:在删除列表中的重复项时,我通常会使用此方法:

First put all the values in a matrix / 2 dimensional list including the key, so the first 3 values would be like this:首先将所有值放在一个矩阵/二维列表中,包括键,所以前 3 个值是这样的:

{
  "image_1.jpg": ['image_xyz.jpg', 'image_abc.jpg'],
  "image_xyz.jpg": ["image_1.jpg", "image_abc.jpg"],
  "image_abc.jpg": ["image_xyz.jpg","image_1.jpg"],
}

would turn into:会变成:

List=[
  ["image_1.jpg","image_xyz.jpg","image_abc.jpg"],
  ["image_xyz.jpg","image_1.jpg","image_abc"],
  ["image_abc.jpg","image_xyz.jpg","image_1.jpg"]
]

make sure the keys are all in the 0th position so that you can save them.确保密钥都在第 0 个 position 中,以便您可以保存它们。

keys=[x[0] for x in List]

then sort the list:然后对列表进行排序:

sorted_list=[sorted(x) for x in List]

then simply compare them using an if statement in a nested for loop and if a list is equal to another then delete it:然后只需使用嵌套 for 循环中的 if 语句比较它们,如果一个列表等于另一个列表,则删除它:

for i in sorted_list:
    for j,k in enumerate(sorted_list):
        if i==k:
            del sorted_list[j] # deleting any equal lists

now that the duplicates are gone and you have the keys convert the list back into a dictionary if preferred现在重复项已经消失了,如果愿意,您可以使用键将列表转换回字典

Overall code if needed:如果需要,整体代码:

List=[
  ["image_1.jpg","image_xyz.jpg","image_abc.jpg"],
  ["image_xyz.jpg","image_1.jpg","image_abc"],
  ["image_abc.jpg","image_xyz.jpg","image_1.jpg"]
]
keys=[x[0] for x in List]
sorted_list=[sorted(x) for x in List]
for i in sorted_list:
    for j,k in enumerate(sorted_list):
        if i==k:
            del sorted_list[j] # deleting any equal lists

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

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