Re: Filtering same elements from two lists
- To: mathgroup at smc.vnet.net
- Subject: [mg67956] Re: Filtering same elements from two lists
- From: Peter Pein <petsie at dordos.net>
- Date: Tue, 18 Jul 2006 05:50:49 -0400 (EDT)
- References: <e9frcp$2kg$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
LectorZ schrieb: > Dear experts, > > I do have to not ordered lists of the following type (same shape and > length): > > list1={{PLUS1, 55, 43}, {PLUS2, 555, 83}, {....},...} > > list2={{..., ...,...}, {..., ...,...},{..., ...,...},{..., > ....,...},{PLUS1,77, 99}, {..., ...,...},{PLUS2, 52,103}, {....}} > > I have to filter the same records according to the 1st element of each > sublist and make a new list with the joint numbers from both lists. > > The result in this case should be: > > resultlist={{PLUS1, 55, 43, 77, 99}, {PLUS2, 555, 83, 52,103}, {....}} > > Thanks, > > LZ > Hi LectorZ, In[1]:= list1 = {{PLUS1, 55, 43}, {PLUS2, 555, 83}, {"some", "thing", "different"}}; list2 = {{PLUS1, 77, 99}, {"something", "completely", "different"}, {PLUS2, 52, 103}}; list3 = Select[Split[Sort[Union[list1, list2]], #1[[1]] === #2[[1]] & ], Length[#1] > 1 & ] Out[3]= {{{PLUS1, 55, 43}, {PLUS1, 77, 99}}, {{PLUS2, 52, 103}, {PLUS2, 555, 83}}} In[4]:= result = (Flatten[{#1[[1,1]], Rest /@ #1}] & ) /@ list3 Out[4]= {{PLUS1, 55, 43, 77, 99}, {PLUS2, 52, 103, 555, 83}} is one of numerous possibilities. Peter