Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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}}};
share|improve this question
1  
Partition is essentially the inverse of Flatten –  bill s Aug 14 '13 at 0:44
    
@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). –  T.T. Aug 14 '13 at 0:46
    
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. –  Jonie Aug 14 '13 at 0:54
    
@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. –  Jacob Akkerboom Aug 17 '13 at 21:26
    
related mathematica.stackexchange.com/q/45293/66 –  faysou Apr 3 at 21:33

4 Answers 4

up vote 22 down vote accepted

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.

share|improve this answer
1  
@halirutan Nice. –  T.T. Aug 14 '13 at 3:20
    
Adding to the nerdiness, can we make unflatten into a nestedRotate by setting f[_] := l1[[++i /. Length@l1 + 1 -> 1]]; or something similar? –  bobthechemist Aug 14 '13 at 16:31
    
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? –  Leonid Shifrin Apr 3 at 17:45
    
@LeonidShifrin I posted an alternative which hopefully doesn't leak any more. Thanks for pinging me on this! –  halirutan Apr 4 at 0:47
    
@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. –  Leonid Shifrin Apr 4 at 10:46

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}]
share|improve this answer

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

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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