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: [mg68306] Re: [mg68230] Replacing Numbers in a List
  • From: Gregory Lypny <gregory.lypny at videotron.ca>
  • Date: Tue, 1 Aug 2006 06:59:24 -0400 (EDT)
  • References: <24981515.1154263195390.JavaMail.root@eastrmwml07.mgt.cox.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Thank you, Bob, Peter, and Jean-Marc.

So many good ways to accomplish this.

	Gregory

On Sun, Jul 30, 2006, at 8:39 AM, Bob Hanlon wrote:

> 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: Eclipse plugin
  • Next by Date: Re: need to make a special function
  • Previous by thread: RE: Re: RE: Eclipse plugin
  • Next by thread: Re: Re: Replacing Numbers in a List