You can read the input into a list
of dict
s, as in my example.
Then, you can use itertools.combinations(data, 2)
to get all the pairs.
import itertools
import random
def get_similarity(obj1, obj2):
# your own get_similarity function using obj1['name'], obj2['id'], etc
# here I'm just returning random number
return random.randint(1, 100)
data = [
{'id': 101, 'name': 'tomato'},
{'id': 102, 'name': 'tomatos'},
{'id': 103, 'name': 'tomatoes'},
{'id': 104, 'name': 'tomato'},
]
print('Item_ID1', '\t', 'Item_ID2', '\t', 'Similarity')
for obj1, obj2 in itertools.combinations(data, 2):
similarity = get_similarity(obj1, obj2)
print(obj1['id'], '\t', obj2['id'], '\t', similarity)
This outputs
Item_ID1 Item_ID2 Similarity
101 102 89
101 103 83
101 104 75
102 103 9
102 104 3
103 104 86
In your sample output, you are repeating the same pair twice (for example, (101, 104) and (104, 101).
If this was intended, you can simply print the same pair twice with the order of objects swapped:
for obj1, obj2 in itertools.combinations(data, 2):
similarity = get_similarity(obj1, obj2)
print(obj1['id'], '\t', obj2['id'], '\t', similarity)
print(obj2['id'], '\t', obj1['id'], '\t', similarity)