MathGroup Archive 2005

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

Search the Archive

Re: splitting sublists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg58087] Re: splitting sublists
  • From: "Stefan Linnik" <- at mk-nntp-1.news.uk.worldonline.com>
  • Date: Sat, 18 Jun 2005 06:07:39 -0400 (EDT)
  • References: <d8rjme$isb$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Here's my suggestion:

f[lst_, char_]:= Module[{sp, sel},
    sp= Split[lst, ( #1 =!= char && #2 =!= char )& ];
    sel= Select[sp, ( #1 =!= {char} )& ];
    If[MemberQ[sp, {char}], sel, First[sel]]
    ]

lst = {{3, 1, 4, "x", 1, 5, 9, 2, "x"}, {6, 5, "x", 3, 5, "x", 8, 9, "x"}};
Map[f[#1, "x"] & , lst, {0, -2}]
:= {{{3,1,4},{1,5,9,2}},{{6,5},{3,5},{8,9}}}

The code can also handle lists of varying depth eg:

lst = {1, "x", {3, 1, 4, "x", 1, 5, 9, 2, "x"}, {6, 5, "x", 3, {1, 2, "x",
4}, 5, "x", 8, 9, "x"}};
Map[f[#1, "x"] & , lst, {0, -2}]
:= {{1},{{{3,1,4},{1,5,9,2}},{{6,5},{3,{{1,2},{4}},5},{8,9}}}}

Not sure whether that's useful or not, but still

Stefan Linnik
~~~~~~~~

<hrocht at mail15.com> wrote in message news:d8rjme$isb$1 at smc.vnet.net...
> hi
> how to split the sublists inside a bigger list wherever we find "x"
> letter
> {{3,1,4,"x",1,5,9,2,"x"},{6,5,"x",3,5,"x",8,9,"x"}}
> the results will be:
>
> {{{3,1,4},{1,5,9,2}},{{6,5},{3,5},{8,9}}}
>
> best regards
> and thanks for this great group
> anton
> 



  • Prev by Date: Re: Transformation rule problem
  • Next by Date: Re: Mathematica 5.0 eps generation
  • Previous by thread: Re: splitting sublists
  • Next by thread: Re: splitting sublists