18

I have the list {a, b, c, d, e, f, g, h, i} and I want to make a sub-list of the form {{{a, b}, c}, {{d, e}, f}, {{g, h}, i}} I've tried using Partition, but I can't see how to use it here.

Sektor
  • 3,320
  • 7
  • 27
  • 36
ProfTC
  • 183
  • 4
  • Hi, welcome to Mathematica.SE, please consider taking the tour so you learn the basic rules of the site. Once you gain enough reputation by making good questions you will be able to vote up and down both questions and answers. Your question has been answered, but its a good idea to wait 24hours for other answers before accepting the best one for you. – rhermans Aug 25 '15 at 15:12
  • When you see good questions and answers, please vote them up by clicking the grey triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. As you receive help, try to give it too, by answering questions in your area of expertise. – rhermans Aug 25 '15 at 15:14

10 Answers10

14

My take:

{Most@#, Last@#} & /@ Partition[{a, b, c, d, e, f, g, h, i}, 3]

@Guesswhoitis:

Transpose[{Drop[#, None, -1], #[[All, -1]]}] & @ Partition[list, 3]

@Pickett

Developer`PartitionMap[{Most[#], Last[#]} &, lst, 3]
LLlAMnYP
  • 11,486
  • 26
  • 65
11

Using the (undocumented) six-argument form of Partition to restructure partition elements:

Partition[{a, b, c, d, e, f, g, h, i}, 3, 3, 1, {}, {{#, #2}, #3} &]

{{{a, b}, c}, {{d, e}, f}, {{g, h}, i}}

Also

SequenceCases[{a, b, c, d, e, f, g, h, i}, {a_, b_, c_}:>{{a, b}, c}]

{{{a, b}, c}, {{d, e}, f}, {{g, h}, i}}

kglr
  • 394,356
  • 18
  • 477
  • 896
9

For those with 10.2, a couple new functions for the heck of it...

BlockMap[TakeDrop[#, 2] /. {l_} :> l &, list, 3]

or

BlockMap[TakeDrop[#, 2]~FlattenAt~2 &, list, 3]

or

With[{k := {{#1, #2}, #3} &}, BlockMap[k @@ # &, list, 3]]
kale
  • 10,922
  • 1
  • 32
  • 69
9

Good problem for the application of argument destructuring.

data = {a, b, c, d, e, f, g, h, i};

restructure[{a_, b_, c_}] := {{a, b}, c}

restructure /@ Partition[data, 3]
{{{a, b}, c}, {{d, e}, f}, {{g, h}, i}}
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
  • @LLlAMnYP. Thanks for pointing out the typo. – m_goldberg Aug 25 '15 at 22:24
  • No problem, some of these days I'd sooner believe an obvious typo is actually something real subtle and intended. Say, what's self : func[args___] := ... construct called? – LLlAMnYP Aug 25 '15 at 22:25
  • @LLlAMnYP. I don't know any name for that pattern. – m_goldberg Aug 25 '15 at 22:27
  • The bot bumped this post today: http://mathematica.stackexchange.com/a/7136/26956 - saw it in the last two lines of code. Your typo reminded me of that. A couple of minutes for chat, maybe? – LLlAMnYP Aug 25 '15 at 22:30
  • @LLlAMnYP. Sorry, can't chat -- late in my time zone -- need to sign out. – m_goldberg Aug 25 '15 at 23:44
  • I really like this answer as for a novice- it is easy to understand- many thanks!! – ProfTC Aug 26 '15 at 00:07
  • @TerenceCosgrove If you feel it's the best one for you, please do accept it. But for interpolation of functions with multidimensional variables the general form is {{{args1},f1},{{args2},f2},...} in that case, a solution that's general enough for any number of elements, not just 3 would suit you better. – LLlAMnYP Aug 26 '15 at 08:03
5

Yet another one for variety:

dat = {a, b, c, d, e, f, g, h, i};

{Partition[dat, 2, 3], dat[[3 ;; ;; 3]]}\[Transpose]
{{{a, b}, c}, {{d, e}, f}, {{g, h}, i}}
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
4

Something with rules:

Partition[{a, b, c, d, e, f, g, h, i}, 3] /. {x_, y_, z_?AtomQ} -> {{x, y}, z}
KennyColnago
  • 15,209
  • 26
  • 62
3
list = {a, b, c, d, e, f, g, h, i};

BlockMap[Apply[{{#1, #2}, #3} &], list, 3]

{{{a, b}, c}, {{d, e}, f}, {{g, h}, i}}

eldo
  • 67,911
  • 5
  • 60
  • 168
1
list = {a, b, c, d, e, f, g, h, i};
Partition[Riffle[Partition[#, 2, 3], Take[#, {3, -1, 3}]], 2] &@list

{{{a, b}, c}, {{d, e}, f}, {{g, h}, i}}

OkkesDulgerci
  • 10,716
  • 1
  • 19
  • 38
1

Another way using MapApply and Partition:

{Most@{##}, #3} & @@@ Partition[#, 3] &@list

({{{a, b}, c}, {{d, e}, f}, {{g, h}, i}})

E. Chan-López
  • 23,117
  • 3
  • 21
  • 44
1

Using TakeList:

Clear["Global`*"];

f[k_List, parts_List] := Module[{tp = Total@parts}, FoldPairList[{TakeList[#1, parts], #1[[tp + 1 ;;]]} & , k , ConstantArray[1, Quotient[Length@k, tp]]] // Map[FlattenAt[-1]] ]

Usage

alist = {a, b, c, d, e, f, g, h, i};
f[alist, {2, 1}]

{{{a, b}, c}, {{d, e}, f}, {{g, h}, i}}

f[Alphabet[], {5, 3}]

{{{"a", "b", "c", "d", "e"}, "f", "g", "h"}, {{"i", "j", "k", "l", "m"}, "n", "o", "p"}, {{"q", "r", "s", "t", "u"}, "v", "w", "x"}}

f[Alphabet[], {2, 2, 1}]

{{{"a", "b"}, {"c", "d"}, "e"}, {{"f", "g"}, {"h", "i"}, "j"}, {{"k", "l"}, {"m", "n"}, "o"}, {{"p", "q"}, {"r", "s"}, "t"}, {{"u", "v"}, {"w", "x"}, "y"}}

Syed
  • 52,495
  • 4
  • 30
  • 85