Re: Simple list operation
- To: mathgroup at smc.vnet.net
- Subject: [mg59463] Re: [mg59420] Simple list operation
- From: Igor Antonio <igora at wolf-ram.com>
- Date: Wed, 10 Aug 2005 02:57:00 -0400 (EDT)
- Organization: Wolfram Research, Inc.
- References: <200508090730.DAA19079@smc.vnet.net>
- Reply-to: igora at wolf-ram.com
- 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 Steve, All you need to use is the Drop function in combination with the Map function at a specific level (or "depth") of the list of elements. This will do what you want: In[4]:= test = {{{4, 5}, {5, 9}, {6, 2}}, {{2, 3}, {3, 5}}, {{7, 1}, {9, 8}, {0, 7}, {2, 8}}, {{4, 3}, {6, 5}}}; You can create a pure function: (Drop[#, -1]) & that will drop the last element of whatever it is applied to. Now you can use Map to apply it to every element of your list. In[5]:= Map[(Drop[#, -1]) & , test, {2}] Out[5]= {{4, 5}, {5, 9}} {{2, 3}} {{7, 1}, {9, 8}, {0, 7}} {{4, 3}} Note that you have to specify Map[..., {2}] because you have a list of a list of pairs (which is a list), so you want to run Map at the second level. -- Igor C. Antonio Wolfram Research, Inc. http://www.wolfram.com To email me personally, remove the dash.
- References:
- Simple list operation
- From: Steve Gray <stevebg@adelphia.net>
- Simple list operation