我有一段 django 代码,用于迭代模型的查询集并删除任何匹配的模型。查询集变大了,这些动作实际上设置为周期性任务,因此速度成为问题。
这是代码,如果有人愿意尝试帮助优化它!
# For the below code, "articles" are just django models
all_articles = [a reallly large list of articles]
newest_articles = [some large list of new articles]
unique_articles = []
for new_article in newest_articles:
failed = False
for old_article in all_articles:
# is_similar is just a method which checks if two strings are
# identical to a certain degree
if is_similar(new_article.blurb, old_article.blurb, 0.9)
and is_similar(new_article.title, old_article.title, 0.92):
failed = True
break
if not failed:
unique_articles.append(new_article)
return unique_articles
谢谢你们!