MathGroup Archive 2012

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

Search the Archive

Re: split the sublists into parts according to some rules

  • To: mathgroup at smc.vnet.net
  • Subject: [mg127947] Re: split the sublists into parts according to some rules
  • From: Bob Hanlon <hanlonr357 at gmail.com>
  • Date: Tue, 4 Sep 2012 05:47:46 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net
  • References: <20120903065630.0E5726897@smc.vnet.net>

To handle vectors

splitToParts[list_?VectorQ, segments_Integer?Positive] :=
 splitToParts[{list}, segments][[1]] /; segments <= Length[list]

splitToParts[list_List, segments_Integer?Positive] :=
 Module[{len,
   revList =
    Select[list, Length[#] >= segments &]}, (len = Floor[Length[#]/segments];
     If[segments*len == Length[#], Partition[#, len],
      Join[{Take[#, len + 1]},
       splitToParts[{Drop[#, len + 1]}, segments - 1][[1]]]]) & /@ revList]



On Mon, Sep 3, 2012 at 2:56 AM, Dana DeLouis <dana01 at me.com> wrote:
> Hi.  I see you have an excellent solution.
> I'm not sure, but would you want the function to work on a vector as well?
>
> splitToParts[{a,b,c,d,e,f,g},2]
> {}
>
> This is not any better, but takes a different approach.
>
> If one had 12 items, and wanted to group them into 5 groups, the size of each group might be:
>
> k=IntegerPartitions[12,{5}]  //Last
> {3,3,2,2,2}
>
> We note that the first 2 items are 1 larger then rest, the count being..
>
> Mod[12,5]
> 2
>
> For the right-side group, we adjust the starting position of Partition, and don't allow overhang of the right.
>
>
> SplitToParts[list_List,n_Integer?Positive]:=Module[
> {v,Fx},
>
> (* Custom Function *)
>
> Fx[v_List,x_Integer?Positive]:=Module[
> {k=Length[v],
> left,
> right},
>
> If[x==1,Return[v]];
> If[x==k,Return[List/@v]];
>
> {m,p}=QuotientRemainder[k,x];
>
> If[p==0,
> Partition[v,k/x],
> (* Else *)
> left = Partition[v,m+1][[;;p]];
> right = Partition[v,m,m,{-p,-1}][[p-x;;]];
> Join[left,right]
> ]
> ];
>
> (* If it's a vector.. *)
> If[Depth[list]==2,
> If[Length[list]<n,Return[{}],Return[Fx[list,n]]]];
>
> (* Else it's a list *)
>
> v=Select[list,Length[#]>=n&];
> If[Length[v]==0,Return[{}]   ];
> Map[Fx[#,n]&,v]
> ]
>
>
> //  End of Function ============
>
> Some test data:
>
> v={{a,b,c,d,e},{x,y,z},{a1,a2,a3,a4}};
>
> SplitToParts[v,3]
> {{{a,b},{c,d},{e}},  {{x},{y},{z}},  {{a1,a2},{a3},{a4}}}
>
> SplitToParts[v,4]
> {{{a,b},{c},{d},{e}},  {{a1},{a2},{a3},{a4}}}
>
> SplitToParts[v,5]
> {{{a},{b},{c},{d},{e}}}
>
> SplitToParts[v,6]
> {}
>
>
> Appears to work with a vector:
>
> v={a,b,c,d,e};
>
> SplitToParts[v,3]
> {{a,b},{c,d},{e}}
>
> SplitToParts[v,4]
> {{a,b},{c},{d},{e}}
>
> SplitToParts[v,6]
> {}
>
> = = = = = = = = = =
> HTH  :>)
> Dana DeLouis
> Mac & Mathematica 8
> = = = = = = = = = =
>
>
>
>
> On Friday, August 31, 2012 4:01:50 AM UTC-4, Joug Raw wrote:
>> Dear all,
>>
>>
>>
>> I have a long list which has many sublists inside,
>>
>>
>>
>> Thelonglist={{a,b,c,d,e},{x,y,z},{a1,a2,a3,a4},...}
>>
>>
>>
>> Each sublist has length > 1(no single element sublist exists) . And the
>>
>> lengths of the sublists are different and unknow in advanced. The length of
>>
>> some of the sublists are odd number, such as {a,b,c,d,e} and {x,y,z}. Some
>>
>> sublists have even number list length, like {a1,a2,a3,a4}.
>>
>>
>>
>> What I want to achieved is to split each sublist into two (or three, or
>>
>> more) parts. In the two parts case, if the length of original sublist is
>>
>> even number, the two new parts will have same length, e.g, {a1,a2,a3,a4}
>>
>> become {{a1,a2},{a3,a4}}. If the sublist has length in odd number,
>>
>> after splitting one of the two parts should have one more element than the
>>
>> other.
>>
>>
>>
>> That is,
>>
>> Input: Thelonglist={{a,b,c,d,e},  {x,y,z},  {a1,a2,a3,a4},...}
>>
>> Output: Newlist={{a,b,c}, {d,e}},  {{x,y}, {z}},  {{a1,a2}, {a3,a4}},...}
>>
>>
>>
>> Or the same idea for the three parts case,
>>
>> Input: Thelonglist={{a,b,c,d,e},  {x,y,z},  {a1,a2,a3,a4},...}
>>
>> Output: Newlist={{{a,b}, {c,d}, {e}},  {{x},{y},{z}},  {{a1}, {a2},
>>
>> {a3,a4}}, ...}
>>
>>
>>
>> For the case of 4 parts, the number 4 is larger than the length of some
>>
>> sublists and I will abandon those list with short length.
>>
>> Input: Thelonglist={{a,b,c,d,e},  {x,y,z},  {a1,a2,a3,a4},...}
>>
>> Output: Newlist={{{a}, {b}, {c}, {d,e}},  {{a1}, {a2}, {a3}, {a4}}, ...}
>>
>>
>>
>> Could there be a simple function to achieve this idea generally? Say, a
>>
>> function like *SplittoPart[**list_*, *partnumber_**]*, in which I just need
>>
>> to give the input list and the number of parts of sublists I want to have.
>>
>> Then it will do the job above. If the number of sublist is larger then the
>>
>> length of some sublists, the function just abandon those short list and do
>>
>> the split(or partition) work on the other lists with long enough length.
>>
>> Could some one help me on this?
>>
>>
>>
>> If that is too complicated, I would still be happy to see some one could
>>
>> give me a solution only for the case of splitting to two parts,
>>
>>
>>
>> Input: Thelonglist={{a,b,c,d,e},  {x,y,z},  {a1,a2,a3,a4},...}
>>
>> Output: Newlist={{a,b,c}, {d,e}},  {{x,y}, {z}},  {{a1,a2}, {a3,a4}},...}
>>
>>
>>
>> Thanks a lot for your kind help!
>
>
>



  • Prev by Date: Re: split the sublists into parts according to some rules
  • Next by Date: Eigensystem bug in Mathematica 7.0.1 on Windows 7 (64 bit) for
  • Previous by thread: Re: split the sublists into parts according to some rules
  • Next by thread: Re: split the sublists into parts according to some rules