Re: Manipulating list
- To: mathgroup at smc.vnet.net
- Subject: [mg101544] Re: [mg101486] Manipulating list
- From: "David Park" <djmpark at comcast.net>
- Date: Thu, 9 Jul 2009 02:01:09 -0400 (EDT)
- References: <208967.1247052114442.JavaMail.root@n11>
Kosal, First, let's change your second list to use small case letters. Mathematica always uses an initial capital for its own symbols and by starting our symbols with small case letters we know we won't inadvertently use a built-in Mathematica symbol. Specifically, in Mathematica I stands for the unit imaginary number. list1 = {{a, b, c}, {d, e, f}, {g, h, i}}; list2 = {{1, 2}, {3, 4}, {5, 6}}; Then we can combine your two list by the following: MapThread[Prepend[#2, Last[#1]] &, {list1, list2}] {{c, 1, 2}, {f, 3, 4}, {i, 5, 6}} MapThread is a functional programming command. If you haven't seen these before they are a little difficult at first but well worth learning because they are so nice. MapThread generates elements of a new list by working itself through the two lists by pairs. Look it up in Help. Prepend[#2, Last[#1]] & is a pure function (Look up Function in Help) that instructs Mathematica how to combine the elements of the two lists. #1 stands for an element from the first list, {a,b,c} say, and #2 stands for an element from the second list, {1,2} say. So Prepend[{1,2},Last[{a,b,c}] gives {c,1,2} etc. David Park djmpark at comcast.net http://home.comcast.net/~djmpark/ From: kosal lee [mailto:leekosal at yahoo.com] Hello Mathematica users 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}} Thank you. Kosal