MathGroup Archive 1999

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

Search the Archive

Re: harder parts of list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg15664] Re: harder parts of list
  • From: "Nicolas B.E. Sawyer" <nbes at eee.nott.ac.uk>
  • Date: Sat, 30 Jan 1999 04:28:57 -0500 (EST)
  • Organization: University of Nottingham
  • References: <786uft$29u@smc.vnet.net> <78pavn$cti@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

This uses select:

In[18]:=
lst={{4,2,3,1},{3,1,5},{2,4,3}}

Out[18]=
{{4, 2, 3, 1}, {3, 1, 5}, {2, 4, 3}}

In[19]:=
Map[(first=#[[1]];Select[#,#<first&])&,lst]

Out[19]=
{{2, 3, 1}, {1}, {}}

Hartmut Wolf wrote:
> 
> Dear Arnold
> 
> Arnold Knopfmacher schrieb:
> >
> > This seems to be a harder list parts problem. Given a list like
> >
> > lst={{4,2,3,1},{3,1,5},{2,4,3}}
> >
> > I wish to select the elements in each sublist that are smaller than the
> > first entry in that sublist.
> > So for the above lst the output should be {{2,3,1},{1},{}}. How can I
> > do this  with Select or Cases?
> >
> > Using pattern matching this works
> >
> > rule1={a_,b___,c_,d___}:>{a,b,d} /;c>a
> >
> > (# //. rule1)& /@ lst
> >
> > {{4,2,3,1},{3,1},{2}}
> >
> > Rest /@ %
> >
> > {{2,3,1},{1},{}}
> >
> > Thanks
> > Arnold Knopfmacher
> > Wiws University
> > South Africa
> 
> You do have a solution with replacement rules, that works. In fact you
> solved for "smaller or equal", as you see with lst2:
> 
>    lst2= {{4,2,3,4},{3,1,5},{1,1,1},{0}}
> 
>    (# //. rule1)& /@ lst2
> 
> then gives:
> 
>    {{4,2,3,4},{3,1},{1,1,1},{0}}
> 
> and then
> 
>    Rest /@ %
> 
> results to:
> 
>    {{2,3,4},{1},{1,1},{}}
> 
> If you want only to keep the "smaller" elements then just throw away
> those greater or equal:
> 
>    rule2={a_,b___,c_,d___}:>{a,b,d} /;c>=a
> 
> which gives
> 
>    (# //. rule2)& /@ lst2
> 
>    {{4,2,3},{3,1},{1},{0}}
> 
> Rest /@ %
> 
>    {{2,3},{1},{},{}}
> 
> You can have a solution with Select, which differs a little bit when
> selecting for elements "smaller or equal" (1) or "smaller" (2).
> 
> For (1) try:
> 
>    Function[x,Rest[Select[x, (# <= x[[1]]) &]]] /@ lst2
> 
>    {{2,3,4},{1},{1,1},{}}
> 
> for (2) Rest could result to an error Rest::norest, but you don't need
> Rest then (because you first sublist elements won't be kept), so
> 
>    Function[x,Select[x, (# < x[[1]]) &]] /@ lst2
> 
>    {{2,3},{1},{},{}}
> 
> This wasn't the case with your replacement rule, since you explicitly
> demanded and needed(!) to keep the first element.
> 
> Perhaps your real problem was to nest two pure functions. Use Function
> to give unambiguous names to the arguments.
> 
> With Cases you don't have that problem, since the other variable is a
> pattern you have to give a name to anyways:
> 
>    Rest[Cases[#, y_ /; y <= #[[1]] ]]& /@ lst2
> 
>    {{2,3,4},{1},{1,1},{}}
> 
> for (1) and for (2) respectively:
> 
>    Cases[#, y_ /; y < #[[1]] ]& /@ lst2
> 
>    {{2,3},{1},{},{}}
> 
> Back to your own solution, of course you can make a one-liner:
> 
>    Rest[# //. rule1]& /@ lst2
> 
>    {{2,3,4},{1},{1,1},{}}
> 
> and
> 
>    Rest[# //. rule2]& /@ lst2
> 
>    {{2,3},{1},{},{}}
> 
> Even more elegantly define
> 
>    selectAccordingTo = Function[x, Rest[x //. #]]&
> 
> then
> 
>    selectAccordingTo[rule1] /@ lst2
> 
>    {{2,3,4},{1},{1,1},{}}
> 
> and
> 
>    selectAccordingTo[rule2] /@ lst2
> 
>    {{2,3},{1},{},{}}
> 
> As often there are a hundred of possibilities, and certainly some are
> more elegant, performant, handy, or ... what you like.
> 
> Hartmut

-- 
Nicolas B.E. Sawyer


Department of Electrical and Electronic Engineering, University of
Nottingham,
University Park,
Nottingham,
NG7 2RD.

Tel: +44 115 9515151 ext 12028
Fax: +44 115 9515616

E-mail: nbes at eee.nottingham.ac.uk


  • Prev by Date: RE: speed
  • Next by Date: Re: How to fix bad EPS output from Display[.., .., "EPS"]
  • Previous by thread: Re: harder parts of list
  • Next by thread: findroot problem