Given that:
g=[[], [], [0, 2], [1, 5], [0, 2, 3, 7], [4, 6], [1, 4, 5, 6], [], [], [3, 7]]
How can I compare each list within g so that for lists sharing anyone common number can merge to a set?
e.g.
0
exists in g[2]
and g[4]
so they merge to a set {0,2,3,7}
I have tried the following but it doesn't work:
for i in g:
for j in g:
if k in i == l in j:
m=set(i+j)
I want to make the largest possible set.
If
g
org
's elements are huge you can use Disjoint Sets to boost efficiency.This data structure can be used to classify each element into the set it should belong to.
The first step is to build a Disjoint Set collection with all
g
sets labeled by their index ing
:Then, the sets gets joined whenever the intersection is not empty:
At this point
dss
gives you a common label for sets ofg
that should joined together:Now you just have to build the new sets joining those which have the same label:
Resulting in:
This is the implementation of Disjoint Sets I used but surely you can find anotherone with a better sintax:
As a much faster way You can first create a list of the set of items with len more than one (
s
) . then go through your list and update in place withunion
function !Demo :
Benchmark with @Mark's answer :
Here's a quickie that will give a list of all the sets that intersect:
Note that each result will be repeated, since each list is being compared twice, once on the left and once on the right.