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: [mg68259] Re: [mg68230] Replacing Numbers in a List
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Mon, 31 Jul 2006 03:45:12 -0400 (EDT)
  • Reply-to: hanlonr at cox.net
  • Sender: owner-wri-mathgroup at wolfram.com

y={122,211,212,221,223,232,322};

x=Table[Random[Integer,{100,325}],{10^5}];

Count[x,222]

452

x1=x;Timing[Do[x1=ReplacePart[x1,222,Position[x1,y[[i]]]],{i,Length@y}];]

{0.450666 Second,Null}

Timing[x2=x/.x_?(MemberQ[y,#]&):>222;]

{1.62676 Second,Null}

Timing[x3=ReplacePart[x,222,Position[x,_?(MemberQ[y,#]&)]];]

{1.55927 Second,Null}

Timing[x4=If[MemberQ[y,#],222,#]&/@x;]

{0.154602 Second,Null}

Count[x1,222]==Count[x2,222]==Count[x3,222]==Count[x4,222]

True

Of these, mapping the If statement is best.


Bob Hanlon

---- Gregory Lypny <gregory.lypny at videotron.ca> 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
> 


  • Prev by Date: Re: Solving linear systems in matrix notation?
  • Next by Date: RE: Replacing Numbers in a List
  • Previous by thread: Re: Replacing Numbers in a List
  • Next by thread: RE: Replacing Numbers in a List