我想比较一个列表的值是否存在于另一个列表的值中。它们很大(50k + 项,来自数据库)。
编辑:
我还想将重复的记录标记为duplicate=True,并将它们保留在表中以供以后引用。
列表是这样的:
n_emails=[db_id,checksum for id,checksum in search_results]
#I want to compare checksum if exist inside same list or other list and retrieve id (db_id , if exist)
#example : n_emails= [[1,'CAFEBABE010'],[2,'bfeafe3df1ds],[3,'deadbeef101'],[5,'CAFEBABE010']]
#in this case i want to retrive id 1 and 5 coz they are same checksum
for m in n_emails:
dups=_getdups(n_emails,m[1],m[0])
n_dups=[casesdb.duplicates.insert( **dup ) for dup in dups]
if n_dups:
print "Dupe Found"
casesdb(casesdb.email_data.id == m[0]).update(duplicated=True)
def _getdups(old_lst,em_md5,em_id):
dups=[]
for old in old_lst:
if em_md5==old[0] and old[1]!=em_id:
dups.append(dict(org_id=old[1],md5hash=old[0],dupID=em_id,))
return dups
但它似乎太长而且在更大的列表中(50k 对 50k 记录+)它运行了超过 5000 秒并且从未完成,似乎永无止境的循环? 我运行的服务器有 4 GB 内存和 4 个内核。显然我做错了什么。
请帮忙..非常感谢!
已解决:
字典索引映射要快得多! (当 mysql 表未被索引时,请注意我没有针对索引表进行测试)。
它的 20 秒 vs 30 毫秒 = 20*1000/30 = 666 次!哈哈
请您参考如下方法:
最快的方法是像这样使用字典:
n_emails= [[1,'CAFEBABE010'],[2,'bfeafe3df1ds'],[3,'deadbeef101'],[5,'CAFEBABE010']]
d = {}
for id, hash in n_emails:
if hash not in d:
d[hash] = [id]
else:
d[hash].append(id)
for hash, ids in d:
if len(ids) > 1:
print hash, ids
这几乎就是散列连接的算法
for hash, count in select hash, count(id) as num from emails group by num having num > 1:
first = None
for index, id in enumerate(select id from emails where hash=hash sort by desc id):
if index == 0:
first = id
continue
update emails set duplicate=first where id=id
这将是 sql/python 解决方案,我采用重复列并使用它来存储这条被认为是重复的消息
emails 表至少是:
create table emails (id, hash, duplicate default null)
