'Iterating through a list of lists with transitive property

I'm looking for a proper way to transform the data:

similarities = [["Parasite", "1917"],
                ["Parasite", "Jojo Rabbit"],
                ["Joker", "Ford v Ferrari"]]

In each sublist there are films that are similar, I need to be able to iterate through it somehow, to count how many similar films each one has. Similar movies have transitive property: if movie 1 is similar to movie 2 and movie 2 is similar to movie 3 -> movie 1 is similar to movie 3 and vice versa.

The outcome is like this: Joker - 1 (Ford v Ferrari) 1917 - 2 (Parasite, Jojo Rabbit) Parasite - 2 (1917, Jojo Rabbit) Jojo Rabbit - 2 (Parasite, 1917) Ford v Ferrari - 1 (Joker) I thought of a dict or a graph traversal, but nothing seems to work so far



Sources

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

Source: Stack Overflow

Solution Source