Re: ReplacePart?
- To: mathgroup@smc.vnet.net
- Subject: [mg10505] Re: ReplacePart?
- From: "P.J. Hinton" <paulh@wolfram.com>
- Date: Tue, 20 Jan 1998 02:22:53 -0500
- Organization: "Wolfram Research, Inc."
Scott Morrison <scott@morrison.fl.net.au> > I have a list of pairs of the form: > gt={{3,5},{{},6},{7,{}},{{},{}},{{},56},{{},3}}; > > The empty lists inside pairs are being used as placeholders. I want to > replace these with the elements of b: b={a1,a2,a3,a4,a5,a6}; hopefully > resulting in the list > {{3,5},{a1,6},{7,a2},{a3,a4},{a5,56},{a6,3}} > > I had thought that ReplacePart could do this, as it says: > "ReplacePart[expr, new, pos, npos] replaces parts at positions pos in > expr by parts at positions npos in new." Give this a try: In[1]:= gt={{3,5},{{},6},{7,{}},{{},{}},{{},56},{{},3}}; In[2]:= b={a1,a2,a3,a4,a5,a6}; In[3]:= q = Transpose @ {b,Position[gt,{}]} Out[3]= {{a1,{2,1}},{a2,{3,2}},{a3,{4,1}},{a4,{4,2}},{a5,{5,1}},{a6,{6,1}}} In[4]:= Fold[ReplacePart[#1,Sequence @@ #2]&,gt,q] Out[4]= {{3,5},{a1,6},{7,a2},{a3,a4},{a5,56},{a6,3}} -- P.J. Hinton Mathematica Programming Group paulh@wolfram.com Wolfram Research, Inc. http://www.wolfram.com/~paulh/ Disclaimer: Opinions expressed herein are those of the author alone.