Re: "expanding a list"
- To: mathgroup at smc.vnet.net
- Subject: [mg57940] Re: [mg57860] "expanding a list"
- From: Ken Levasseur <klevasseur at mac.com>
- Date: Mon, 13 Jun 2005 05:51:01 -0400 (EDT)
- References: <200506100629.CAA15653@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
My first reaction was to use a pattern and rule.
In[56]:=
expandlistRule[j_, k_] :=
{{a_, b___} :> Join[{Table[a, {j}]},
(Table[#1, {k}] & ) /@ {b}]}
In[57]:=
Range[5] /. expandlistRule[4, 2]
Out[57]=
{{1, 1, 1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}}
In[58]:=
Range[1] /. expandlistRule[4, 2]
Out[58]=
{{1, 1, 1, 1}}
In[59]:=
Range[5] /. expandlistRule[0, 2]
Out[59]=
{{}, {2, 2}, {3, 3}, {4, 4}, {5, 5}}
In[61]:=
Range[5] /. expandlistRule[2, 0]
Out[61]=
{{1, 1}, {}, {}, {}, {}}
On Jun 10, 2005, at 2:29 AM, Mark Coleman wrote:
> Greetings,
>
> I'd like to construct a list as follows. Say I have a list of reals
> {x1,x2,x3,...xn}, and another list of integers {r,s}. I'd like to
> make a new list
>
> {{x1,x1,x1,...},{x2,x2,x2....},{x3,x3,x3,...},....,{xn,xn,xn,...}},
> where the first sublist is of length r, and the remaining sublists
> are of length s.
>
>
> Thanks,
>
> -Mark
>
>
>
>
Ken Levasseur
http://homepage.mac.com/klevasseur/
- References:
- "expanding a list"
- From: Mark Coleman <mark@markscoleman.com>
- "expanding a list"