35
$\begingroup$

Imagine we have a list with some hierarchical structure:

list = {{{2,3},{2,4}},{{{3,4}}},{{5,6},{7,8}}};

Which we then flatten to some level in order to process the data points:

Flatten[list,1]
>> {{2, 3}, {2, 4}, {3, 4}, {5, 6}, {7, 8}}

We then do "something" to change the value of the elements in the list, without increasing or decreasing the number of elements:

>> {{5*10^9, 3}, {2, 4}, {3, 4}, {191991, 6}, {7, 8}}

Can we reverse the flattening procedure to go back to a list of the form:

>> {{{5*10^9,3},{2,4}},{{{3,4}}},{{191991,6},{7,8}}};
$\endgroup$
7
  • 2
    $\begingroup$ Partition is essentially the inverse of Flatten $\endgroup$
    – bill s
    Commented Aug 14, 2013 at 0:44
  • $\begingroup$ @bill s Oh, I know about Partition. My question is how we can kind of save the previous structure of the list prior to flattening, and then reapply this hierarchical structuring (which can pretty much be arbitrary). $\endgroup$
    – T.T.
    Commented Aug 14, 2013 at 0:46
  • $\begingroup$ I'd presume you can't actually save the previous structure. What you might be able to do is simply map the new elements back to list, ie. replacing elements in the old list with ones in the new list in order. This should be possible since you aren't increasing or decreasing the number of elements. $\endgroup$
    – Jonie
    Commented Aug 14, 2013 at 0:54
  • $\begingroup$ @bills I feel Internal-Deflatten is also a candidate for the inverse of Flatten, and is worth mentioning :). See Szabolcs answer in the Q&A about undocumented functions. But yeah I guess it doesn't really help. $\endgroup$ Commented Aug 17, 2013 at 21:26
  • 3
    $\begingroup$ Code golf post inspired by this question: codegolf.stackexchange.com/questions/43018/unflatten-an-array $\endgroup$ Commented Dec 31, 2014 at 18:37

5 Answers 5

36
$\begingroup$

Edit

As described here, the solution below leaks symbols in the global namespace. This can be prevented by using this shorter equivalent:

unflatten[l_, o_] := Module[{i = 1, l1 = Flatten[l]},
  Function[Null, l1[[i++]], {Listable}][o]
]

Original

You can unflatten every list with the same number of elements as long as you still have your original list. When you really overwrite your original list with its flattened version, then the structure is lost forever.

Let me give an alternative approach which should work with all kinds of elements. Additionally, this function might have a nerdy touch, because it is not quite obvious why it does what it does

unflatten[l_, o_] := Module[{f, i = 1, l1 = Flatten[l]},
  Attributes[f] = {Listable}; f[_] := l1[[i++]]; f[o]]

When we now take a somehow deeply structured list orig, we can restructure the list {1,2,3,4} in exactly the same way

orig = {{{}, {Exp[1]}, 3, {{{{a}}}, c}}};
unflatten[Range[4], orig]

(* {{{}, {1}, 2, {{{{3}}}, 4}}} *)

Spoiler alert

OK, to give some hints: Above we want to structure the list l exactly the same way as o. The function f doesn't really do something. Every time it is called with any argument, it returns the next element from the list l by using the counter i. The important thing is, that f has the attribute Listable which makes that it is first distributed to all elements in the list, no matter how deeply they are nested. If f finally meets a list element, it returns the replacement element and the list structure is preserved. Therefore, the whole approach works because Listable functions return the same list structure as their arguments and the order in which the elements are visited is the correct one.

$\endgroup$
6
  • 2
    $\begingroup$ @halirutan Nice. $\endgroup$
    – T.T.
    Commented Aug 14, 2013 at 3:20
  • $\begingroup$ Adding to the nerdiness, can we make unflatten into a nestedRotate by setting f[_] := l1[[++i /. Length@l1 + 1 -> 1]]; or something similar? $\endgroup$ Commented Aug 14, 2013 at 16:31
  • $\begingroup$ A memory / symbol leak has been discovered for your code - which is otherwise very neat and elegant, here. Perhaps, it is a good idea to update to one of the alternatives posted there, or some other one? $\endgroup$ Commented Apr 3, 2014 at 17:45
  • 1
    $\begingroup$ @halirutan Welcome :) Actually, the minimal change would've been just using SetAttributes[f, Listable] in place of Attributes[f]={Listable}, but of course there are many ways, as usual in M. $\endgroup$ Commented Apr 4, 2014 at 10:46
  • 1
    $\begingroup$ (Just to put it here, too) The reason f leaks is that you've destroyed the Temporary attribute $\endgroup$
    – b3m2a1
    Commented Apr 14, 2020 at 7:23
6
$\begingroup$

I faced the same problem a while ago. Here's my initial code:

copyPartition[A_, B_] := Module[{i = 0}, Map[A[[++i]]&,B,{-1}]]  

It works fine most of the time:

A = {a,b,c,d,e,f,g,h,i};  
B = {1, {2, 3}, {4, {5}, 6}, {7, {{8}, {9}}}};  
copyPartition[A,B]
(*  {a, {b, c}, {d, {e}, f}, {g, {{h}, {i}}}} *)

But change the i in A to i$:

A = {a,b,c,d,e,f,g,h,i$};
copyPartition[A,B]
(*  {a, {b, c}, {d, {e}, f}, {g, {{h}, {8}}}} *)

The i$ got evaluated -- a situation that's not likely to occur in practice, but I still didn't like it.
My solution was to do everything twice:

copyPartishun[A_, B_] := Map[A[[#]]&, Block[{i = 0}, Map[++i&,B,{-1}]], {-1}]

That solves the problem, but it's slow, so I asked in Mathgroup.
The best answer was from J. Siehler:

partitionedAz[A_, B_] := ReplacePart[B, A, Position[B,_,{-1},Heads->False],
                                     Transpose@{Range@Length@Flatten@B}]
$\endgroup$
6
$\begingroup$

Internal`CopyListStructure

Examples:

orig = {{{}, {Exp[1]}, 3, {{{{a}}}, c}}};

Internal`CopyListStructure[orig, Range[4]]
{{{}, {1}, 2, {{{{3}}}, 4}}}
list = {{{2, 3}, {2, 4}}, {{{3, 4}}}, {{5, 6}, {7, 8}}};
list1 = Flatten[list, 1];
list1[[1]] = {2, 8};

Internal`CopyListStructure[list, Flatten@list1]
 {{{2, 8}, {2, 4}}, {{{3, 4}}}, {{5, 6}, {7, 8}}}
$\endgroup$
5
$\begingroup$

One possible solution

list = {{{2, 3}, {2, 4}}, {{{3, 4}}}, {{5, 6}, {7, 8}}}

{{{2, 3}, {2, 4}}, {{{3, 4}}}, {{5, 6}, {7, 8}}}

list1 = Flatten[list, 1]
list1[[1]] = {2, 8};
list1

{{2, 3}, {2, 4}, {{3, 4}}, {5, 6}, {7, 8}}

{{2, 8}, {2, 4}, {{3, 4}}, {5, 6}, {7, 8}}

ReplacePart[list, {Sequence @@ 
    MapThread[#1 -> #2 & , {Position[list, _?(NumberQ[#] &)] , 
    Flatten[list1]}]}]

{{{2, 8}, {2, 4}}, {{{3, 4}}}, {{5, 6}, {7, 8}}}

$\endgroup$
4
$\begingroup$

I prefer the following code. You don't bother to copy the structure; just replace the number in the original list with corresponding one.

lstOrigin = {{{2, 3}, {2, 4}}, {{{3, 4}}}, {{5, 6}, {7, 8}}};
answerFlatten = Flatten@{{5*10^9, 3}, {2, 4}, {3, 4}, {191991, 6}, {7, 8}};

Block[{i = 1},
   answerFinal = lstOrigin /. _?NumberQ :> (answerFlatten[[i++]])]

The code gives

{{{5000000000, 3}, {2, 4}}, {{{3, 4}}}, {{191991, 6}, {7, 8}}}

If you do this often, you may probably change the Block into a function.

$\endgroup$
1
  • $\begingroup$ +1 This seems like a very straightforward approach that guarantees that the structure will be maintained. $\endgroup$
    – DavidC
    Commented Jan 1, 2015 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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