Re: List manipulation
- To: mathgroup at smc.vnet.net
- Subject: [mg73267] Re: List manipulation
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Fri, 9 Feb 2007 02:21:03 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <eqep4g$5dk$1@smc.vnet.net>
sepp wrote:
> hello,
> I would like to combine list elements in the following way:
>
> I have a list with
> liste={{a1,a2,a3},{b1,b2,b3},{c1,c2,c3},.....}
> and a single element {e}
>
>
> I would to add the element {e} to each sublist to get something like
> liste_new={ {a1,a2,a3,e},{b1,b2,b3,e},{c1,c2,c3,e},.....}
> so that finally I can apply a function f on the list
> liste_manipulated={f[ {a1,a2,a3,e}],f[{b1,b2,b3,e}],f[{c1,c2,c3,e}],.....}
>
> I had a look at thread, mapthread, inner, outer, but nothing really
> seems to fit...
> thank you in advance for useful comments!
>
>
The following expression should fulfill your needs.
In[1]:=
lst = {{a1, a2, a3}, {b1, b2, b3}, {c1, c2, c3}, {d1, d2, d3}};
In[2]:=
f /@ Transpose[Join[Transpose[lst], {Table[e, {Length[lst]}]}]]
Out[2]=
{f[{a1, a2, a3, e}], f[{b1, b2, b3, e}], f[{c1, c2, c3, e}], f[{d1, d2,
d3, e}]}
The following in/out are just to describe how it works.
In[3]:=
Length[lst]
Out[3]=
4
In[4]:=
Transpose[lst]
Out[4]=
{{a1, b1, c1, d1}, {a2, b2, c2, d2}, {a3, b3, c3, d3}}
In[5]:=
Table[e, {Length[lst]}]
Out[5]=
{e, e, e, e}
In[6]:=
Join[Transpose[lst], Table[e, {Length[lst]}]]
Out[6]=
{{a1, b1, c1, d1}, {a2, b2, c2, d2}, {a3, b3, c3, d3}, e, e, e, e}
In[7]:=
Join[Transpose[lst], {Table[e, {Length[lst]}]}]
Out[7]=
{{a1, b1, c1, d1}, {a2, b2, c2, d2}, {a3, b3, c3, d3}, {e, e, e, e}}
In[8]:=
Transpose[Join[Transpose[lst], {Table[e, {Length[lst]}]}]]
Out[8]=
{{a1, a2, a3, e}, {b1, b2, b3, e}, {c1, c2, c3, e}, {d1, d2, d3, e}}
In[9]:=
f /@ Transpose[Join[Transpose[lst], {Table[e, {Length[lst]}]}]]
Out[9]=
{f[{a1, a2, a3, e}], f[{b1, b2, b3, e}], f[{c1, c2, c3, e}], f[{d1, d2,
d3, e}]}
Regards,
Jean-Marc