Re: Building clusters out of a nested list
- To: mathgroup at smc.vnet.net
- Subject: [mg114965] Re: Building clusters out of a nested list
- From: Peter Pein <petsie at dordos.net>
- Date: Fri, 24 Dec 2010 04:13:13 -0500 (EST)
- References: <iev2sj$4uf$1@smc.vnet.net>
On 23.12.2010 09:56, Dmitry wrote: > Dear All, > > please, help me find a solution to the following problem: > > suppose I have a list of the form { {A,B,C},{C,D},{E,F},{F,H}}. I need > to build a list whose entries are unions of all entries of the > original list with nonzero intersection. For the example above, the > end result is {{A,B,C,D},{E,F,H}}. > > Thanks a lot! > Hi Dimitry, this can easily be done using ReplaceRepeated In[1]:= rule={x___List,a1_List,y___List,a2_List,z___List}/;Intersection[a1,a2]=!={}:>{x,Union[a1,a2],y,z}; In[2]:= {{A,B,C},{C,D},{E,F},{F,H}}//.rule Out[2]= {{A,B,C,D},{E,F,H}} In[3]:= SeedRandom[20101224]; Table[RandomInteger[{1,100},{RandomInteger[{2,6}]}],{10}] Out[3]= {{56,98},{48,54},{47,1,9},{62,99,47,91,66,41},{35,81},{39,40,39},{34,52,30,22,43,85},{85,92,24,73},{6,34,25,96,72,78},{93,26,16,66}} In[4]:= %//.rule Out[4]= {{56,98},{48,54},{1,9,16,26,41,47,62,66,91,93,99},{35,81},{39,40,39},{6,22,24,25,30,34,43,52,72,73,78,85,92,96}} to check this result: In[5]:= Intersection@@@Partition[%,2,1] Out[5]= {{},{},{},{},{}} OK :-) Cheers, Peter