Re: Simple list operation
- To: mathgroup at smc.vnet.net
- Subject: [mg59474] Re: Simple list operation
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Wed, 10 Aug 2005 02:57:48 -0400 (EDT)
- Organization: The Open University, Milton Keynes, U.K.
- References: <dd9mte$isv$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Steve Gray wrote: > Not having used Mathematica for months, I have forgotten some of the little I knew. > > I have a list of lists like this: > > test = { {{4, 5}, {5, 9}, {6, 2}}, > {{2, 3}, {3, 5}}, > {{7,1}, {9, 8}, {0, 7}, {2, 8}}, > {{4, 3}, {6, 5}}}; > test // ColumnForm > {{{4, 5}, {5, 9}, {6, 2}}, > {{2, 3}, {3, 5}}, > {{7, 1}, {9, 8}, {0, 7}, {2, 8}}, > {{4, 3}, {6, 5}}} > > What I want to do is just remove the last element in each sublist, with this result: > > {{{4, 5}, {5, 9}}, > {{2, 3}}, > {{7, 1}, {9, 8}, {0, 7}}, > {{4, 3}}} > > There must be some simple functional way to do this, but I haven't found it. > I will appreciate any tips. Thank you. > > Steve Gray > Hi Steve, You can map either *Most* or *Drop* (with a negative index) on your data: In[1]:= test = {{{4, 5}, {5, 9}, {6, 2}}, {{2, 3}, {3, 5}}, {{7, 1}, {9, 8}, {0, 7}, {2, 8}}, {{4, 3}, {6, 5}}} Out[1]= {{{4, 5}, {5, 9}, {6, 2}}, {{2, 3}, {3, 5}}, {{7, 1}, {9, 8}, {0, 7}, {2, 8}}, {{4, 3}, {6, 5}}} In[2]:= (Most[#1] & ) /@ test Out[2]= {{{4, 5}, {5, 9}}, {{2, 3}}, {{7, 1}, {9, 8}, {0, 7}}, {{4, 3}}} In[3]:= (Drop[#1, -1] & ) /@ test Out[3]= {{{4, 5}, {5, 9}}, {{2, 3}}, {{7, 1}, {9, 8}, {0, 7}}, {{4, 3}}} Best regards, /J.M.