# How to partition a list according to a nested table structure?

This question is related to this quesion. The goal is to use ParallelMap to map a function to a nested Table to get the FinestGrained distribution of calculation. But after that, I want to recover the original nested data structure instead of a flat one.

For example, say I have a nested Table like this

Table[
Table[{line, honeycombnum, distance}, {line, 1,
honeycombnum + 1}, {distance, 1, 4}], {honeycombnum, 2, 5}]


as you can see, this nested table structure is not of equal length .

Then what is the general way to recover the data structure after using

ParallelMap[f[#[], #[], #[]] &, Flatten[Table[
Table[{line, honeycombnum, distance}, {line, 1,
honeycombnum + 1}, {distance, 1, 4}], {honeycombnum, 2, 5}], 2], Method -> "FinestGrained"]


By the word "general ", I mean the method should be suitable for arbitrary complex Table structure.

A possibility is the following:

it = Flatten@MapIndexed[#2 -> #1 &, t, {-2}];
if[a_ -> b_] := a -> f[b];
res = ParallelMap[if, it];
ReplacePart[t, res]


It adds to the table the index of every object, it creates a new function working like yours but preserving the indices, operates with parallel map and then substitutes all the parts in the original table with the results.

I think this is rather general and perhaps rather terse.

• Thank you! But I can't understand. Can you explain a little? What is t,it,if? Nov 10, 2013 at 10:01
• Oh, I understand Nov 10, 2013 at 10:07
• Yes, I am sorry for the excessive synthesis...: "t" is the table, "it" is the indexed table, "if" is the index-preserving function based on f. The final line produces the result Nov 10, 2013 at 19:04

I'm sure there should be an easier way. Meanwhile:

orig = Table[ Table[{line, honeycombnum, distance}, {line, 1, honeycombnum + 1},
{distance, 1, 4}], {honeycombnum, 2, 5}];
(* Form a template for the re-structure op, some pattern to match your data needed*)
i = 0; orig1 = orig /. {_Integer, _Integer, _Integer} :> x[++i]
(* Let's see how it looks when flattened *)
orig2 = Flatten[orig1]
dest = Flatten[orig /. {a_Integer, b_, c_} -> {a + b + c}] (*Something like your ParallelMap *)
dest1 = orig1 /. Thread[orig2 -> dest] (* The re-structured result*)


Note that you need a pattern to match your data for this method to work