python - 消除自定义数据结构的重复

标签 python

class md5Check(object):
    """docstring for md5Check"""
    def __init__(self, md5, fullpath):
        super(md5Check, self).__init__()
        self.fullpath = fullpath
        self.md5 = md5

    fullpath = ""
    md5 = ""

imageFiles = list()
temp = md5Check(md5Sum, fullpath)
imageFiles.append(temp)

我想删除我的列表中的重复项,该列表由我的 md5Check 数据结构组成。类实例 md5 变量可以识别重复项。删除重复项的好方法是什么?

最佳答案

由于 md5 是可哈希的,因此您可以使用 set 来跟踪看到的 md5 值。

seen = set()
imageFiles = [x for x in imageFiles if x.md5 not in seen and not seen.add(x.md5)]

如果您不喜欢副作用:

seen = set()
imageFiles_new = []
for x in imageFiles:
    if x.md5 not in seen:
        imageFiles_new.append(x)
        seen.add(x.md5)
imageFiles = imageFiles_new

关于python - 消除自定义数据结构的重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24639363/

相关文章:

python - 阶乘非零数字不匹配

python - 如何计算具有条件的连续 Pandas 数据帧行之间的天差

Python装饰器理论

python - 如何正确使用Flask的jsonify()返回json?

python - 在两个值之间进行选择并设置 pandas 数据框中最常见的值

python - 如何在 Tkinter 的屏幕上将窗口居中?

python - 使用 "__import__"从字符串变量导入模块给出的结果与普通导入语句不同

python - matplotlib.pyplot 格式字符串 kwarg?

python - Pandas 将时间列添加到日期索引

python - Lektor 分页 - TemplateSyntaxError : Encountered unknown tag 'endblock'