Re: Partition List
- To: mathgroup at smc.vnet.net
- Subject: [mg8455] Re: Partition List
- From: Juergen.Schmidt at math.uni-giessen.de (Jürgen Schmidt)
- Date: Sat, 30 Aug 1997 00:43:09 -0400
- Organization: Math. Inst. der JLU Giessen
- Sender: owner-wri-mathgroup at wolfram.com
In article <5tu41r$2b8 at smc.vnet.net>, "Xah" <xah at best.com> wrote:
>A question on programing.
>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}}.
>
>How to do this using structure manipulating operators, and how to do it with
>pattern matching? Thanks.
>
> Xah, xah at best.com
> http://www.best.com/~xah/SpecialPlaneCurves_dir/specialPlaneCurves.html
> Mountain View, CA, USA
>
The following is a possible solution (Note that the index is shifted by one
due to the deletion of the first element in L which is not of interest)
Juergen
splitList[L_List, patt_] :=
Module[{pos},
pos =
Flatten[ Position[ Delete[L, 1], patt , {1}, Heads -> False ] ];
(Take[L, #]&) /@
Thread at {Prepend[pos + 1, 1], Append[pos, Length[L]]}
];
test1={3, 7, 10, 6, 1, 1, 6, 1, 7, 10, 3, 4, 6};
test2={a, 7, Pi, 6, a^a, EllipticK[1/2], x^2, 7, Sin[K], 3, 4, 3.14};
splitList[test1, 10]
{{3,7},{10,6,1,1,6,1,7},{10,3,4,6}}
splitList[test1, 1]
{{3,7,10,6},{1},{1,6},{1,7,10,3,4,6}}
splitList[test1, a_?OddQ]
{{3},{7,10,6},{1},{1,6},{1},{7,10},{3,4,6}}
splitList[test1, a_?EvenQ]
{{3,7},{10},{6,1,1},{6,1,7},{10,3},{4},{6}}
splitList[test2, a_?EvenQ]
{{a,7,Pi},{6,a^a,EllipticK[1/2],x^2,7,Sin[K],3},{4,3.14}}
splitList[test2, a_?AtomQ]
{{a},{7},{Pi},{6,a^a,EllipticK[1/2],x^2},{7,Sin[K]},{3},{4},{3.14}}
splitList[test2, a_Sin]
{{a,7,Pi,6,a^a,EllipticK[1/2],x^2,7},{Sin[K],3,4,3.14}}
splitList[test2, a_?NumericQ]
{{a},{7},{Pi},{6,a^a},{EllipticK[1/2],x^2},{7,Sin[K]},{3},{4},{3.14}}
splitList[test2, a_?NumberQ]
{{a},{7,Pi},{6,a^a,EllipticK[1/2],x^2},{7,Sin[K]},{3},{4},{3.14}}
_/_/_/_/ _/_/ _/_/_/ _/_/_/ _/_/_/_/ _/_/ _/_/_/ _/_/_/
_/_/
_/_/ Dipl.-Phys. Juergen Schmidt
_/_/ Mathematisches Institut (Math. Physik)
_/_/_/ Justus-Liebig-Universitaet Giessen
_/_/_/ Arndtstr. 2 * D-35392 Giessen
_/_/_/_/
_/_/_/_/_/ phone ++49 (0) 641 99 320-64 (fax -29 / secr. -61)
_/_/_/
_/_/ _/_/ Website Computeralgebra & Mathematical Physics:
_/_/ _/_/ http://www.uni-giessen.de/www-Mathematische-Physik/
_/_/_/_/
_/_/_/_/_/_/ _/_/_/ _/_/_/_/ _/_/_/_/_/ _/_/_/_/_/_/