从字典中删除重复项的更快算法

2024-09-27 09:34:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用Python2.7开发notMINST数据库,试图删除重复的图像。 我将每个图像转换为MD5散列,并创建一个字典图像\u散列

第一种方法是有效的,然而,数据集中总共有500000张图像,几乎花了一个小时。你知道吗

image_hash_identical = {}
for key,value in image_hash.items():
    if value not in image_hash_identical.values():
        image_hash_identical[key] = value

我尝试使用“set”函数创建第二种方法来加快速度:

image_hash_set_values = list(set(image_hash.values()))
for i in range(len(image_hash_set_values)):
    for j in range(i, len(image_hash)):
        image_hash[j] == image_hash_set_values[i]:
            image_hash_identical[i] = image_hash[j]
            break

但是,此代码未能加快进程,因为“set”函数洗牌了图像\u哈希的顺序。有什么方法可以通过“set”函数或任何更快的算法来抑制洗牌吗?你知道吗


Tags: 方法key函数in图像imageforlen

热门问题