MathGroup Archive 1999

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

Search the Archive

Re: Partitioning lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg20535] Re: [mg20439] Partitioning lists
  • From: "David Park" <djmp at earthlink.net>
  • Date: Sat, 30 Oct 1999 00:13:53 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

Mark,

Your method looks pretty good: Here are some alternative methods and timings.

a={1,2,3,4,5,6};

Your method:
TwoPartSplit0[a_] := Table[{Take[a, i], Drop[a, i]}, {i, 1, Length[a] - 1}]
Do[TwoPartSplit0[a], {1000}] // Timing
{0.6 Second, Null}

This is a method using the Partition function;
TwoPartSplit1[a_List] := 
  With[{len = Length[a]}, 
    Table[If[i <= len/2, 
        Partition[a, len - i, len - i, {len + 1 - 2i, len - i}, {}], 
        Partition[a, i, i, {1, len - i}, {}]], {i, 1, len - 1}]]
Do[TwoPartSplit1[a], {1000}] // Timing
{2.86 Second, Null}

This is a method taking the parts:
TwoPartSplit2[a_List] := 
  With[{len = Length[a]}, 
    Table[{a[[Range[1, i]]], a[[Range[1 + i, len]]]}, {i, 1, len - 1}]]
Do[TwoPartSplit2[a], {1000}] // Timing
{1.37 Second, Null}

So Take and Drop seem to be relatively efficient.

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/


>I would like a function f to split a list as follows
>
>a={1,2,3,4,5,6}
>f[a] =
>  {
>    {{1},{2,3,4,5,6}},
>    {{1,2},{3,4,5,6}},
>    {{1,2,3},{4,5,6}},
>    {{1,2,3,4},{5,6}},
>    {{1,2,3,4,5},{6}}
>  }
>
>I can do this as
>
>      Table[{Take[a, i], Drop[a, i]}, {i, 1, Length[a] - 1}]
>
>but it *looks* as though it is very inefficient. Is it?
>It also looks as though there should be a way of using Partition simply to
>chop a list between positions i and i+1, but
>I cannot see how to do it. Any suggestions?
>
>
>mark diamond
>no spam email: markd at psy dot uwa dot edu dot au
>




  • Prev by Date: Looking for Mathematica Related Content
  • Next by Date: CSV Format
  • Previous by thread: Re: Partitioning lists
  • Next by thread: Re: Partitioning lists