Re: Re: Pattern matching more than once
- To: mathgroup@smc.vnet.net
- Subject: [mg12171] Re: [mg12096] Re: Pattern matching more than once
- From: "Jürgen Tischer" <jtischer@pitagoras.univalle.edu.co>
- Date: Fri, 1 May 1998 03:08:24 -0400
Happily (for me) your solution doesn't work for the following example, Paul: l={{a,b,c,d,e},{f,b,i,g,d,h}} The thing of course is that the lengths of those lists aren't equal. Jürgen -----Original Message----- From: Paul Abbott <paul@physics.uwa.edu.au> To: mathgroup@smc.vnet.net Subject: [mg12171] [mg12096] Re: Pattern matching more than once >ASARI Hirotsugu wrote: > >> I have been stuck with the following problem for about 10 days. I can't >> think of an elegant solution. >> >> Input: Two lists, from some universal set Output: Many "spliced" lists >> obtained from the input. >> >> e.g. {{a,b,c},{d,e,f}} --> {{a,b,c},{d,e,f}} (no common element) >> >> {{a,b,c},{d,b,f}} --> {{a,b,c},{d,b,f},{a,b,f},{d,b,c}} >> >> {{a,b,c,d,e},{f,b,g,d,h}} --> >> {{a,b,c,d,e},{a,b,c,d,h},{a,b,g,d,e},{a,b,g,d,h}, >> {f,b,c,d,e},{f,b,c,d,h},{f,b,g,d,e},{f,b,g,d,h}} > >I think that the following code: > >In[1]:=extendList[l_List]:= Module[{tr=Union/@Transpose[l]}, > If[tr==Transpose[l],l,tr=Outer[List,Sequence@@tr]; > Flatten[tr,Length[Dimensions[tr]]-2]]] > >which relies on Transpose (to pair up elements from each list) Union (to >check for common elements) and Outer (to construct all allowed >combinations) does what you want: > >In[2]:= extendList[{{a,b,c},{d,e,f}}] Out[2]= {{a,b,c},{d,e,f}} >In[3]:= extendList[{{a, b, e}, {d, b, f}}] Out[3]= >{{a,b,e},{a,b,f},{d,b,e},{d,b,f}} In[4]:= >extendList[{{a,b,c,d,e},{f,b,g,d,h}}] Out[4]= >{{a,b,c,d,e},{a,b,c,d,h},{a,b,g,d,e},{a,b,g,d,h}, > {f,b,c,d,e},{f,b,c,d,h},{f,b,g,d,e},{f,b,g,d,h}} > >I also feel that there should be a cleverer pattern-matching solution, >probably using ReplaceList ... > >Cheers, > Paul > >____________________________________________________________________ >Paul Abbott Phone: +61-8-9380-2734 >Department of Physics Fax: +61-8-9380-1014 >The University of Western Australia Nedlands WA 6907 >mailto:paul@physics.uwa.edu.au AUSTRALIA >http://www.pd.uwa.edu.au/~paul > > God IS a weakly left-handed dice player >____________________________________________________________________ >