Re: Best code to match corresponding list items?
- To: mathgroup at smc.vnet.net
- Subject: [mg27757] Re: [mg27729] Best code to match corresponding list items?
- From: BobHanlon at aol.com
- Date: Wed, 14 Mar 2001 04:06:55 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
There is a built-in function, it is called Transpose.
n = 5;
listA = Array[a, {n}];
listB = Array[b, {n}];
listC = Array[c, {n}];
listD = Array[d, {n}];
Transpose[{listA, listB, listC, listD}]
{{a[1], b[1], c[1], d[1]}, {a[2], b[2], c[2], d[2]},
{a[3], b[3], c[3], d[3]}, {a[4], b[4], c[4], d[4]},
{a[5], b[5], c[5], d[5]}}
Bob Hanlon
In a message dated 2001/3/13 4:17:11 AM, rison at ix.netcom.com writes:
>Given two lists
>A={a1,a2,a3,...}
>B={b1,b2,b3...}
>
>I want to produce the list
>C={ {a1,b1}, {a2,b2}, {a3,b3}...} }
>as efficiently as possible.
>Better yet, generalize to do this with N lists all of same length.
>
>Obviously it can be done directly with Table, but is there a more elegant,
>implicit way to write it that doesn't involve passing a function to Table
>and using an explicit index? Is there a general functional primitive for
>this that I simply haven't found? Seems like something so common, there
>ought to be a built-in function for it.
>