Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: Partitioning a list from an index

  • To: mathgroup at smc.vnet.net
  • Subject: [mg56999] Re: Partitioning a list from an index
  • From: "Peltio" <peltio at trilight.zone>
  • Date: Thu, 12 May 2005 02:32:44 -0400 (EDT)
  • References: <d5mv78$e05$1@smc.vnet.net>
  • Reply-to: "Peltio" <peltioNOSPAM at despammed.com.invalid>
  • Sender: owner-wri-mathgroup at wolfram.com

"Guy Israeli" wrote

>    list1={1,2,3,4,5,6,7,8,9,10}
>
>and i might want to split it to pairs or threes from index 6 for example,
>sofor threes it will be
>
>    {1,2,{3,4,5},{6,7,8},9,10}

>for pairs it will be
>
>    {1,{2,3},{4,5},{6,7},{8,9},10}

If you want to 'spread' the partitioning from a point pos inside your list,
I'd suggest to work on two separate lists: the first one, from the beginning
to position pos-1 will have to be reversed.
You can then apply Partition, with or without padding to each one of this
lists.
To obtain the above two results, you only need this procedure:

PartitionAt[mylist_List, pos_Integer, dim_Integer] :=
  Module[
    {lista, listb, rem1, rem2, p1, p2, l = Length[mylist]},
        lista = Take[mylist, pos - 1]; listb = Drop[mylist, pos - 1];
        rem1 = Take[lista, Mod[pos - 1, dim]];
        rem2 = Take[listb, -Mod[l - pos + 1, dim]];
        p1 = Reverse[Reverse /@ Partition[Reverse[lista], dim]];
        p2 = Partition[listb, dim];
        Join[rem1, p1, p2, rem2]
    ]

    PartitionAt[{1,2,3,4,5,6,7,8,9,10},6,2]
        {1,{2,3},{4,5},{6,7},{8,9},10}
    PartitionAt[{1,2,3,4,5,6,7,8,9,10},6,3]
        {1,2,{3,4,5},{6,7,8},9,10}
    PartitionAt[{1,2,3,4,5,6,7,8,9,10},7,4]
        {1,2,{3,4,5,6},{7,8,9,10}}
    PartitionAt[{1,2,3,4,5,6,7,8,9,10},8,4]
        {1,2,3,{4,5,6,7},8,9,10}

If you want to take advantage of the more advanced signatures of Partition
you'd have to modify the code to accept more parameters so that they can be
passed to Partition acting on the two lists (this is easily done by adding
an others___ argument in the call to PartitionAt, or by writing different
procedured for the different signatures), and removing the lines that select
and add the remnants of the list. In fact, if you use padding, you would not
need to add the hangovers to your result. The code gets more complicated in
this case, since Partition can have a lot of tweaks.
This is an *untested* (and probably wrong) attempt

PartitionAt[mylist_List, pos_Integer, dim_Integer, others___] :=
  Module[
    {lista, listb, p1, p2, l = Length[mylist]},
    lista = Take[mylist, pos - 1];
    listb = Drop[mylist, pos - 1];
    p1 = Reverse[Reverse /@ Partition[Reverse[lista], dim, others]];
    p2 = Partition[listb, dim, others];
    Join[p1, p2]
    ]

An evident problem with this approach is that overhanging would take
place on each of the two lists, separatedly. But padding should work...

cheers,
Peltio
Invalid address in reply-to. Crafty demunging required to mail me.






  • Prev by Date: Re: NIntegrate-FindRoot acting up in version 5.1
  • Next by Date: Re: NIntegrate-FindRoot acting up in version 5.1
  • Previous by thread: Re: Partitioning a list from an index
  • Next by thread: Re: Ordering broken on Windows, but not Mac