Re: Manipulating list
- To: mathgroup at smc.vnet.net
- Subject: [mg101568] Re: Manipulating list
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Fri, 10 Jul 2009 06:46:01 -0400 (EDT)
On 7/8/09 at 7:13 AM, leekosal at yahoo.com (kosal lee) wrote:
>I am new to Mathematica and I have one particular problem I would
>like you to help me. Suppose I have two lists:
>list1 = {{A,B,C},{D,E,F},{G,H,I}} list2 = {{1,2},{3,4},{5,6}}
>I want to have the third elements in each of member list in list1
>(that is, C,F,I) to appear in the first position in each member list
>in list2
>that is, i want to have list ={{C,1,2},{F,3,4},{I,5,6}}
Here are a couple of ways to accomplish what you want
In[1]:= list1 = {{a, b, c}, {d, e, f}, {g, h, i}};
list2 = {{1, 2}, {3, 4}, {5, 6}};
In[4]:= MapThread[Join[{Last@#1}, #2] &, {list1, list2}]
Out[4]= {{c, 1, 2}, {f, 3, 4}, {i, 5, 6}}
In[6]:= ArrayFlatten@{{List /@ list1[[All, 3]], list2}}
Out[6]= {{c, 1, 2}, {f, 3, 4}, {i, 5, 6}}
Note, I changed the variable names from uppercase to lowercase.
In general, it is not a good idea to use single uppercase
letters as variable names since will often conflict with
built-in symbols. Specifically, both E and I have built-in
meaning. If you make a habit of naming your functions and
variables so that the first letter is lower case, you are
guaranteed not to have a conflict with built-in objects.