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: [mg13994] Re: Q:efficient list operation wanted
  • From: Allan Hayes <hay at haystack.demon.co.uk>
  • Date: Sat, 12 Sep 1998 16:59:17 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

Klaus Duellmann wrote:
> 
> 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

Klaus:

L1 = {1,2,3,4,5,3,7,2,3,9} ;
L2 = {a, b, c};

L1[[Flatten[Position[L1,3]]]]= L2;

L1

{1,2,a,4,5,b,7,2,c,9}


Allan

-- 
Allan Hayes
Training and Consulting
Leicester, UK
hay at haystack.demon.co.uk
http://www.haystack.demon.co.uk
voice: +44 (0)116 271 4198
fax: +44 (0)116 271 8642



  • Prev by Date: Re: list referencing with [[]][[]]
  • Next by Date: Symbolic Matrix Inversion -- What's the best method for Rank under 100?
  • Previous by thread: Re: list referencing with [[]][[]]
  • Next by thread: Re: Q:efficient list operation wanted