Xah's partition question

• To: mathgroup at smc.vnet.net
• Subject: [mg8443] Xah's partition question
• From: wself at viking.emcmt.edu (Will Self)
• Date: Sat, 30 Aug 1997 00:42:54 -0400
• Sender: owner-wri-mathgroup at wolfram.com

```Xah asks for the following:

>I want to partition a list, using special values in the list as boundary
>indicators. For example, myList={3,7,10,6,1,1,6,1,7,10,3,4,6} and my special
>value for boundary is 10, then my desired result would be
>result={{3,7},{10,6,1,1,6,1,7},{10,3,4,6}}.

Here is one way.  The idea is to start with the fact that
Position[myList, 10, 1] gives the list{{3}, {10}}.  Use this list to generate
the list  takeLocations = {{1,2}, {3,9}, {10,13}}; then

Take[myList,#]& /@ takeLocations

will give the desired result.

The following function does it:

xahPartition[someList_, separator_]:=
Module[{
v=Union[{1},Flatten[Position[someList, separator, 1]]]},
Take[someList,#]&/@
Append[Transpose[{Drop[v,-1],Rest[v]-1}],{Last[v],Length[someList]}]]

This works correctly whether or not separator occurs as the first
element of someList, and gives the desired result if separator does not
occur in the top level of someList.

Will Self

```

• Prev by Date: Help with custom Format
• Next by Date: Default Format for Matrices
• Previous by thread: Help with custom Format
• Next by thread: Default Format for Matrices