Re: a tricky List manipulation problem
- To: mathgroup at smc.vnet.net
- Subject: [mg16405] Re: a tricky List manipulation problem
- From: Lawrence Walker <lwalker701 at earthlink.net>
- Date: Thu, 11 Mar 1999 02:17:04 -0500
- Organization: Morgan State University: COMSARE
- References: <7bnqie$d38@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi Adalbert,
I am not sure how my code compares speedwise but see session
below. Also I think I could've use the Select function but
I needed to use it with the Map function. However I didn't
know how to deal with the nested pure functions:
Map[{#,Select[l1, (#1==#)&],Select[l2, (#1==#)}]}&,tags].
So I leave you with...
In[19]:=
l1={{"a",17},{"b",17},{"f",4},{"i",8},{"j",12},{"z",3}};
l2={{"a",2},{"b",17},{"c",17},{"d",16},{"e",16},{"f",9},{"g",2},{"h",17}};
In[30]:=
tags=Intersection[Transpose[l1][[1]] ,Transpose[l2][[1]]]
Out[30]=
{"a","b","f"}
In[31]:=
tagForms = Apply[Alternatives,tags]
Out[31]=
"a"|"b"|"f"
In[39]:=
Select[l1,MemberQ[#,tagForms]&]
Out[39]=
{{"a",17},{"b",17},{"f",4}}
In[40]:=
Select[l2,MemberQ[#,tagForms]&]
Out[40]=
{{"a",2},{"b",17},{"f",9}}
In[42]:=
Transpose[{tags,Transpose[Select[l1,MemberQ[#,tagForms]&]][[2]],
Transpose[Select[l2,MemberQ[#,tagForms]&]][[2]]}]
Out[42]=
{{"a",17,2},{"b",17,17},{"f",4,9}}
Hope this helps,
Lawrence
hanssen at zeiss.de wrote:
>
> Hi, MathGroup
>
> suppose I have two lists of lists which are structured like this:
>
> l1={{s1,x1},....,{si,xi}},
> l2={{t1,y1},....,{tk,yk}}.
>
> where the lengths of the two lists may be different. The first
> entries in the lists serve als "Tags", the second are "values".
> Suppose, no tag appears twice in l1 or twice in l2.
>
> I am looking for the list of elements {sh,xh,yh} of elements
> with the following conditions:
>
> {sh,xh} is element of l1 AND
> {sh,yh] is element of l2 (i.e. combine elements with same "tag")
>
> Without loss of generality suppose, both lists are sorted by their tags.
>
> A procedural approach to this problem would be to climb
> down the ladder starting with {i,j}={1,1}, comparing pairs l1[[i,1]] and l2[[j,1]]
> and taking found tag matches to a result list. This requires some caution,
> since tags from l1 may be missing in l2 and vice versa.
>
> My question is, if there is a better pattern matching or set
> manipulation approach to this problem, since the lists l1 and l2
> might become quite large. Timing for this is the main concern.
> I am looking for a solution which avoids any loop construct.
>
> Best regards
>
> Dipl.-Math. Adalbert Hanszen
--
------------------------------------------------------------
(\___/) The fear of the LORD is the beginning of
(o\ /o) wisdom: a good understanding have all they
/|:.V.:|\ that do his commandments: his praise
\\:::::// endureth for ever. Psa 111:10
-----`"" ""`------------------------------------------------
Lawrence A. Walker Jr., M.Eng./Ph.D. Candidate
Morgan State University
Clarence M. Mitchell School of Engineering
COMSARE (Center Of Microwave/Satellite And RF Engineering)
Rm: 306-Schafer Phone: (443)885-1453
------------------------------------------------------------