'Find Jaccard similarity between multiple list

Let's say I have 3 list:

l1 = ["a", "b", "c"]
l2 = ["c", "e", "f"]
l3 = ["c", "b", "a"]

For Jaccard similarity, I'm using the following function:

def jaccard_similarity(list1, list2):
    intersection = len(list(set(list1).intersection(list2)))
    union = (len(set(list1)) + len(set(list2))) - intersection
    return float(intersection) / union

How can I calculate the Jaccard similarity for all combinations, that is:

(l1,l1), (l1,l2), (l1, l3)
(l2,l1), (l2,l2), (l2, l3) 
(l3,l1), (l3,l2), (l3, l3)

I want to avoid doing this manually for each pair of lists. Also, the final output needs to be a 3x3 matrix.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source