Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1998
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1998

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

Search the Archive

Re: Q: efficient list operation wanted

  • To: mathgroup at smc.vnet.net
  • Subject: [mg14002] Re: Q: efficient list operation wanted
  • From: buttgereit at netcologne.de (Peter Buttgereit)
  • Date: Sat, 12 Sep 1998 16:59:24 -0400
  • Organization: Dipl.-Sportl. Peter Buttgereit
  • References: <6tbvmg$mv1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

[This followup was posted to comp.soft-sys.math.mathematica and a copy 
was sent to the cited author.]

Hi Klaus,
how about


ReplaceByStack[in_List,crit_,stack_List]:=Module[
 {tmp,inrep=in,st=stack},
 tmp=Sequence@@#&/@Position[in,crit];(*get positions of elements to be
                                       repaced;apply Sequence[] to have 
                                       format compatible with Part[]*)
 If[
    Length[tmp]>Length[stack],
    Message[
            ReplaceByStack::stack,Length[tmp],Length[stack]
    ]
 ];                                   (*Error message if replacements
are 
                                        incomplete*)
 (inrep[[ #[[1]] ]]=
            Replace[                 (*make list of replacements and*)
              inrep[[ #[[1]] ]],     (*do replace element by element*)
              #[[2]]
            ])&/@Table[
                  {tmp[[i]], crit->stack[[i]]},
                  {i,1,Min[Length[stack],Length[tmp]]}
                 ];
		inrep                   (*return the modified list*) ];

In[9]:=
ReplaceByStack::stack=
  "Stack given has Length `2`, replacements needed: `1`.\n
		Only the first `2` hits were replaced.";

In[1]:=
L1 = {1,2,3,4,5,3,7,2,3,9}

Out[1]=
{1,2,3,4,5,3,7,2,3,9}

In[2]:=
L2 = {a, b, c}

Out[2]=
{a,b,c}

In[7]:=
ReplaceByStack[L1,3,L2]

Out[7]=
{1,2,a,4,5,b,7,2,c,9}

In[8]:=
ReplaceByStack[L1,3,{a,b}]

ReplaceByStack::"stack": 
    "Stack given has Length 2, replacements needed: 3.
     Only the first 2 hits were replaced."

Out[8]=
{1,2,a,4,5,b,7,2,3,9}

Well ? not really without a loop since I still need the Table[] command.
However, the lists of elements to be replaced and the replacement stack
somehow have to be "aligned", coping with unequal length of the lists. 
So, I think, in this problem we won't find an efficient way doing
without  a loop.
Cheers,
Peter


In article <6tbvmg$mv1 at smc.vnet.net>, k.duellmann at uni-mannheim.deXXX 
says...
> I am looking for an efficient solution of the following problem:
> 
> Given two lists L1 = {1,2,3,4,5,3,7,2,3,9} and L2 = {a, b, c}
> 
> I want to replace all numbers 3 in L1 by the elements of L2
> successively,
> that is the first 3 by a, the second 3 by b and the third 3 by c. That
> is I want to get the following modified L1-list: {1,2,a,4,5,b,7,2,c,9}.
> 
> Replace[] unfortunately replaces elements with a single element only. Is
> there a chance to avoid hideous Do-Loops?
> 
> Any hints are appreciated!
> 
> PS. Remove XXX from my e-mail-address
> 
> K. Duellmann
> 
> 


  • Prev by Date: Re: Select x s.t. y>10
  • Next by Date: Re: list referencing with [[]][[]]
  • Previous by thread: Re: Q: efficient list operation wanted
  • Next by thread: Re: Q: efficient list operation wanted