How can I compare each object with each and if ratio() > 0.7 set possible_duplicate=True
for both objects?
如何将每个对象与每个对象进行比较,如果两个对象的ratio()> 0.7 set possible_duplicate = True?
My try:
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 个解决方案
#1
5
You can use itertools.combinations
to get comparison combinations:
您可以使用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()
#2
2
In your code you are comparing the object to itself. To compare all objects with each other you can use itertools.combinations
在您的代码中,您将对象与自身进行比较。要将所有对象相互比较,可以使用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()
#1
5
You can use itertools.combinations
to get comparison combinations:
您可以使用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()
#2
2
In your code you are comparing the object to itself. To compare all objects with each other you can use itertools.combinations
在您的代码中,您将对象与自身进行比较。要将所有对象相互比较,可以使用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()