我不熟悉Python
,但我喜欢这门语言!在
我有一个巨大的sqlite3
数据库,其中包含row 0
中文件的路径,以及{
我需要根据它们的MD5
搜索重复文件,我想将这些重复文件组织为dictionaries
,如下所示:
{"b23e5d453643f66b68634d0204884cdf":an array of all paths that have the same MD5, like the one that is the key of this dictionary}
我使用以下代码搜索数据库并生成tuples
:
结果如下:
[[u'b23e5d453643f66b68634d0204884cdf'], [u'/Volumes/Backup/images_to_test/File_one_copy.png']]
[[u'b23e5d453643f66b68634d0204884cdf'], [u'/Volumes/Backup/images_to_test/File_one.png']]
[[u'f0b4108172c50f243d9e0132df4703a0'], [u'/Volumes/Backup/images_to_test/File_with_no_duplicate.png']]
我尝试过的每一个可能的解决方案都非常充分,而且性能非常糟糕。最好的Python方法是什么?在
谢谢你!在
如果我没听错,你想要这样的东西:
这非常适合defaultdict(从Python2.5开始提供)
^{pr2}$如果要按MD5组合,则首先要按MD5对列表进行排序。在您的例子中,这最好留给sqlite,因此您应该在查询中添加orderby(参见https://mariadb.com/kb/en/order-by-clause/)。之后,您应该遍历所有行并按照以下行执行操作:
您可以将
cur
放入一个循环中,只检索实际使用的列,并在循环中使用元组解包,如下所示:另外,为什么不使用
^{pr2}$DISTINCT
?在相关问题 更多 >
编程相关推荐