Re: Simple list operation
- To: mathgroup at smc.vnet.net
- Subject: [mg59482] Re: [mg59420] Simple list operation
- From: János <janos.lobb at yale.edu>
- Date: Wed, 10 Aug 2005 02:58:14 -0400 (EDT)
- References: <200508090730.DAA19079@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On Aug 9, 2005, at 3:30 AM, 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 > Here is a tip. In[32]:= a = Table[i, {i, 1, Length[test]}] Out[32]= {1, 2, 3, 4} In[20]:= b = (Length[#1] & ) /@ test - 1 Out[20]= {2, 1, 3, 1} In[33]:= c = Thread[{a, b}] Out[33]= {{1, 2}, {2, 1}, {3, 3}, {4, 1}} In[37]:= Table[Take[test[[c[[i,1]]]], c[[i,2]]], {i, 1, Length[test]}] Out[37]= {{{4, 5}, {5, 9}}, {{2, 3}}, {{7, 1}, {9, 8}, {0, 7}}, {{4, 3}}} I am sure there is a simpler way to do it. János ---------------------------------------------- Trying to argue with a politician is like lifting up the head of a corpse. (S. Lem: His Master Voice)
- References:
- Simple list operation
- From: Steve Gray <stevebg@adelphia.net>
- Simple list operation