Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: repeating elements in a list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg54918] Re: [mg54878] repeating elements in a list
  • From: "David Park" <djmp at earthlink.net>
  • Date: Sun, 6 Mar 2005 00:56:09 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

lst = {A, B, C}

repeatn[n_] := Sequence @@ Table[#, {n}] &

repeatn[3] /@ lst
{A, A, A, B, B, B, C, C, C}

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/ 



From: Torsten Coym [mailto:torsten.coym at eas.iis.fraunhofer.de]
To: mathgroup at smc.vnet.net


Hello,

suppose I have list

lst={A, B, C}

and I want to create a new list, where all elements are repeated n times 
so that (for n=3):

newlst={A, A, A, B, B, B, C, C, C}

I have the following code to do this:

n=3;
newlst=Flatten[Table[Table[lst[[i]], {n}], {i, Length[lst]}]];

but I'm pretty sure, that there must be a more elegant way to solve the 
problem.


thank you




  • Prev by Date: Re: repeating elements in a list
  • Next by Date: Re: Re: Help Mathematica
  • Previous by thread: Re: repeating elements in a list
  • Next by thread: Re: repeating elements in a list