MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Filtering same elements from two lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg67951] Re: [mg67935] Filtering same elements from two lists
  • From: János <janos.lobb at yale.edu>
  • Date: Tue, 18 Jul 2006 05:50:38 -0400 (EDT)
  • References: <200607171051.GAA02334@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On Jul 17, 2006, at 6:51 AM, LectorZ wrote:

> 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


Here is a newbie approach.  It first joins the two lists and sorts it.

I use just one letter labels

In[1]:=
alphabet = {"a", "b", "c",
     "d", "e", "f", "g", "h",
     "i", "j", "k", "l", "m",
     "n", "o", "p", "q", "r",
     "s", "t", "u", "v", "w",
     "x", "y", "z"};

Let's construct the lists first.
In[2]:=
lst1 = Table[
     {If[Mod[i, Length[
          alphabet]] == 0,
       "a", alphabet[[
        Mod[i, Length[
          alphabet]]]]],
      Random[Integer,
       {1, 1000}], Random[
       Integer, {1, 1000}]},
     {i, 1, 100}];

In[3]:=
lst2 = Table[
     {If[Mod[i, Length[
          alphabet]] == 0,
       "a", alphabet[[
        Mod[i, Length[
          alphabet]]]]],
      Random[Integer,
       {1, 1000}], Random[
       Integer, {1, 1000}]},
     {i, 1, 100}];

Here I Joint the lists and sort it.

In[4]:=
jslst = Sort[Join[lst1, lst2]]

Here comes the program that first selects all the elements that has  
the first label than merges the label and the integers belonging to  
the label.
It does it for every label.  At the end you can drop those labels  
that has no integer value assigned to them.  I do not do that step here.

In[5]:=
Table[Flatten[{alphabet[[i]],
     Cases[Flatten[Select[
        jslst, #1[[1]] ==
          alphabet[[i]] & ]],
      _Integer]}],
   {i, 1, Length[alphabet]}]


János


----------------------------------------------
Trying to argue with a politician is like lifting up the head of a  
corpse.
(S. Lem: His Master Voice)


  • Prev by Date: Re: Why Does Repeated Dot Product Take So Long?
  • Next by Date: Re: Clustering as constraint solving
  • Previous by thread: Filtering same elements from two lists
  • Next by thread: Re: Filtering same elements from two lists