MathGroup Archive 2006

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

Search the Archive

Re: Replacing Numbers in a List

  • To: mathgroup at smc.vnet.net
  • Subject: [mg68258] Re: Replacing Numbers in a List
  • From: "Scout" <Scout at nodomain.com>
  • Date: Mon, 31 Jul 2006 03:45:11 -0400 (EDT)
  • References: <eahsgh$op1$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

"Gregory Lypny" <gregory.lypny at videotron.ca>
news:eahsgh$op1$1 at smc.vnet.net...
> Hello everyone,
>
> I have a long list of integers called x, and I want to replace those
> that are members of list y with the number 222.
>
> I did it this way with a Do loop.
>
> y = {122, 211, 212, 221, 223, 232, 322};
> Do[x = ReplacePart[x,222, Position[x, y[[i]]]],{i, Length@y} ]
>
> Is this OK, or is there a more direct way?
>
> Regards,
>
> Gregory
>

Hi Gregory,
instead of the Do loop, try this:

    In[1]:= x=Range[10]
    Out[1]= {1,2,3,4,5,6,7,8,9,10}
    In[2]:= y={2,7,9};

    In[3]:= ReplacePart[x,222,Position[x,#][[1]]&/@y]
    Out[3]= {1,222,3,4,5,6,222,8,222,10}

Hope this helps,
    ~Scout~


  • Prev by Date: Re: Inserting Blank Lists
  • Next by Date: Re: {x},{y} -> {x,y} ?
  • Previous by thread: Re: Replacing Numbers in a List
  • Next by thread: Re: Replacing Numbers in a List