Re:expanding a list
- To: mathgroup at smc.vnet.net
- Subject: [mg42609] Re:[mg42593] expanding a list
- From: Alan Kelly <Alan.Kelly at tcd.ie>
- Date: Fri, 18 Jul 2003 05:25:02 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
My thanks to Mihajlo Vanevic and Bob Hanlon for answering my question
(see below). I asked for an elegant and efficient solution and
that's what I got!
Mihajlo suggested:
MapThread[Table[#1, {#2}] &, {plist, flist}]
or
Flatten@MapThread[Table[#1, {#2}] &, {plist, flist}]
and Bog suggested:
Flatten[Table[#[[1]], {#[[2]]}] & /@ Transpose[{plist, flist}]]
Again, many thanks to both. Now all I have to do is understand how
these actually work!!
Alan Kelly
My original question
_______________________________________________________________
Hi all,
I believe that this is a rather elementary question, but I am not
aware of a simple, elegant and efficient solution. I have searched
the MathGroup archive, but with no success.
I have two lists - both of the same length. The first contains a
list of probabilities. For example, suppose the list length is 4
(although in practice it would be much longer) and is of the
following form:
plist = {0.252, 0.252, 0.257, 0.235}.
The second contains a list of integers:
flist = {7,5,6,4}.
I wish to take the first element of plist and replicate that value
using the first element of flist. Similarly for the remaining
elements.
I should finish with a list of length 7+5+6+4 or 22.
Any suggestion please.
Many thanks,
Alan Kelly
Trinity College Dublin
+-------------------------------------------------------+
Dr. Alan Kelly E-mail akelly at mail.tcd.ie
Biostatistician Phone: +353-1-608 1385 or 608 1087
Fax: +353-1-403 1211 or 403 1212
Director of the Small Area Health Research Unit
Department of Community Health & General Practice
Trinity Center for Health Sciences
AMNCH
Tallaght Hospital
Dublin 24
Ireland
http://www.tcd.ie/Community_Health/SAHRU/index.html
Please note that electronic mail to, from or within the College, may be the
subject of a request under the Freedom of Information Act.
+--------------------------------------------------------+
My thanks to Mihajlo Vanevic and
<fontfamily><param>Geneva</param><smaller>Bob
Hanlon</smaller></fontfamily> for answering my question (see below). I
asked for an elegant and efficient solution and that's what I got!
Mihajlo suggested:
MapThread[Table[#1, {#2}] &, {plist, flist}]
or
Flatten@MapThread[Table[#1, {#2}] &, {plist, flist}]
and Bog suggested:
<fontfamily><param>Geneva</param><smaller>Flatten[Table[#[[1]],
{#[[2]]}] & /@ Transpose[{plist, flist}]]
</smaller></fontfamily>Again, many thanks to both. Now all I have to
do is understand how these actually work!!
Alan Kelly
My original question
_______________________________________________________________
Hi all,
I believe that this is a rather elementary question, but I am not
aware of a simple, elegant and efficient solution. I have searched
the MathGroup archive, but with no success.
I have two lists - both of the same length. The first contains a
list of probabilities. For example, suppose the list length is 4
(although in practice it would be much longer) and is of the
following form:
plist = {0.252, 0.252, 0.257, 0.235}.
The second contains a list of integers:
flist = {7,5,6,4}.
I wish to take the first element of plist and replicate that value
using the first element of flist. Similarly for the remaining
elements.
I should finish with a list of length 7+5+6+4 or 22.
Any suggestion please.
Many thanks,
Alan Kelly
Trinity College Dublin
+-------------------------------------------------------+
Dr. Alan Kelly E-mail akelly at mail.tcd.ie
Biostatistician Phone: +353-1-608 1385 or 608 1087
Fax: +353-1-403 1211 or 403 1212
Director of the Small Area Health Research Unit
Department of Community Health & General Practice
Trinity Center for Health Sciences
AMNCH
Tallaght Hospital
Dublin 24
Ireland
http://www.tcd.ie/Community_Health/SAHRU/index.html
Please note that electronic mail to, from or within the College, may be the
subject of a request under the Freedom of Information Act.
+--------------------------------------------------------+