15
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – rhermans Aug 25 '15 at 15:12
  • $\begingroup$ 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. $\endgroup$ – rhermans Aug 25 '15 at 15:14
5
$\begingroup$

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}}
$\endgroup$
  • $\begingroup$ @LLlAMnYP. Thanks for pointing out the typo. $\endgroup$ – m_goldberg Aug 25 '15 at 22:24
  • $\begingroup$ 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? $\endgroup$ – LLlAMnYP Aug 25 '15 at 22:25
  • $\begingroup$ @LLlAMnYP. I don't know any name for that pattern. $\endgroup$ – m_goldberg Aug 25 '15 at 22:27
  • $\begingroup$ The bot bumped this post today: 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? $\endgroup$ – LLlAMnYP Aug 25 '15 at 22:30
  • $\begingroup$ @LLlAMnYP. Sorry, can't chat -- late in my time zone -- need to sign out. $\endgroup$ – m_goldberg Aug 25 '15 at 23:44
13
$\begingroup$

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]
$\endgroup$
  • 1
    $\begingroup$ Equivalent: Transpose[{Drop[#, None, -1], #[[All, -1]]}] & @ Partition[list, 3]. $\endgroup$ – J. M. is away Aug 25 '15 at 15:26
  • $\begingroup$ @J. M. True... but for what purpose? (I mean, faster, or something else?) $\endgroup$ – LLlAMnYP Aug 25 '15 at 15:28
  • 1
    $\begingroup$ It can also be written Developer`PartitionMap[{Most[#], Last[#]} &, lst, 3], which might be faster. $\endgroup$ – C. E. Aug 25 '15 at 16:00
  • $\begingroup$ @Pickett Oooh, regardless, how similar to normal built-in functions, this should be an answer. $\endgroup$ – LLlAMnYP Aug 25 '15 at 16:22
  • 1
    $\begingroup$ I just presented an approach that is not very different (which is why I left a comment and not an answer). $\endgroup$ – J. M. is away Aug 25 '15 at 16:48
7
$\begingroup$

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]]
$\endgroup$
5
$\begingroup$

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}}

$\endgroup$
3
$\begingroup$

Something with rules:

Partition[{a, b, c, d, e, f, g, h, i}, 3] /. {x_, y_, z_?AtomQ} -> {{x, y}, z}
$\endgroup$
3
$\begingroup$

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}}
$\endgroup$
0
$\begingroup$
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}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.