如何比较每个对象?

时间:2016-03-30 09:08:54

标签: python django

如何将每个对象与每个对象进行比较以及如果ratio()> 0.7为这两个对象设置possible_duplicate=True

我的尝试:

from difflib import SequenceMatcher


class Item(models.Model):
   name = models.CharField(max_length=255)
   desc = models.TextField()
   possible_duplicate = models.BooleanField(default=False)



items = Item.objects.all()

for item in items:
    obj = Item.objects.get(pk=item.pk)
    similarity = SequenceMatcher(None, item.desc, obj.desc).ratio()
    if similarity > 0.7:
        item.possible_duplicate = True
        item.save()
        obj.possible_duplicate = True
        obj.save()

2 个答案:

答案 0 :(得分:5)

您可以使用itertools.combinations获取比较组合:

>>> import itertools
>>> items = [1, 2, 3]
>>> itertools.combinations(items, 2)  # 2 -> yields tuples with 2 items
<itertools.combinations object at 0x7f5e456d5ba8>
>>> list(itertools.combinations(items, 2))
[(1, 2), (1, 3), (2, 3)]
import itertools

items = Item.objects.all()

for item1, item2 in itertools.combinations(items, 2):
    similarity = SequenceMatcher(None, item1.desc, item2.desc).ratio()
    if similarity > 0.7:
        for item in item1, item2:
            item.possible_duplicate = True
            item.save()

答案 1 :(得分:2)

在您的代码中,您将对象与自身进行比较。要将所有对象相互比较,您可以使用itertools.combinations

items_list = list(Items.objects.all())

for a,b in itertools.combinations(items_list, 2):
    similarity = SequenceMatcher(None, a.desc, b.desc).ratio()
    if similarity > 0.7:
        a.possible.duplicate = True
        a.save()
        b.possible.duplicate = True
        b.save()