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: [mg68253] Re: Replacing Numbers in a List
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Mon, 31 Jul 2006 03:45:05 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <eahsgh$op1$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Gregory Lypny wrote:
> 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
> 
What about transformation rules?

  x= { 221,223,222,221,223,223,224,220,122,223}
  y= { 122,211,212,221,223,232,322};

  x/. p_/; MemberQ[ y,p]->222

returns {222, 222, 222, 222, 222, 222, 224, 220, 222, 222}

HTH,
Jean-Marc


  • Prev by Date: Re: Inserting Blank Lists
  • Next by Date: Re: multiple regression
  • Previous by thread: Re: Replacing Numbers in a List
  • Next by thread: Re: Replacing Numbers in a List