MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Simple list operation

  • To: mathgroup at smc.vnet.net
  • Subject: [mg59469] Re: Simple list operation
  • From: "dkr" <dkrjeg at adelphia.net>
  • Date: Wed, 10 Aug 2005 02:57:25 -0400 (EDT)
  • References: <dd9mte$isv$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

In[55]:=
test ={{{4, 5},{5, 9},{6, 2}},{{2, 3},{3, 5}},{{7,1},{9, 8},{0, 7},{2,
          8}},{{4, 3}, {6, 5}}};

Using ReplaceAll, note that the following will NOT work, because the
Mathematica pattern matcher will attempt, by default, to match the
largest subexpression, which in this case will be the entire test
expression:

In[56]:=
test/.{b___,c_}:>{b}
Out[56]=
{{{4,5},{5,9},{6,2}},{{2,3},{3,5}},{{7,1},{9,8},{0,7},{2,8}}}

The following will work:

In[57]:=
test/.{b___,{_?NumberQ,___}}:>{b}
Out[57]=
{{{4,5},{5,9}},{{2,3}},{{7,1},{9,8},{0,7}},{{4,3}}}

Here the NumberQ restriction prevents the pattern from matching the
entire expression.

Another alternative would be:

In[58]:=
test/.{a__}:>dum[a]/.{b___,c_}:>{b}/.a_dum:>List@@a
Out[58]=
{{{4,5},{5,9}},{{2,3}},{{7,1},{9,8},{0,7}},{{4,3}}}

Here the outermost List head is temporarily replaced by dum, preventing
the pattern {b___,c_} from matching the entire expression.


  • Prev by Date: Re: Simple list operation
  • Next by Date: Re: Mathematica goes Bad
  • Previous by thread: Re: Simple list operation
  • Next by thread: Re: Simple list operation