Re: Any simple way to flatten all but the bottom level?

• To: mathgroup at smc.vnet.net
• Subject: [mg72354] Re: Any simple way to flatten all but the bottom level?
• From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
• Date: Tue, 26 Dec 2006 07:51:08 -0500 (EST)
• Organization: The Open University, Milton Keynes, UK
• References: <emb4dh\$950\$1@smc.vnet.net>

```dontdont at gmail.com wrote:
> Am I overlooking something I should be able to see?
>
> When I Nest Table I sometimes produce things like this
>
> {{0, 0, 0}, {{0, 0, 13}, {0, 1, 9}, {0, 2, 5}, {0, 3, 1}}, {{1, 0, 4},
> {1, 1, 0}}}
>
> where each triple is actually one solution I'm looking for.
> But I don't see a simple way to transform this result into
>
> {{0, 0, 0}, {0, 0, 13}, {0, 1, 9}, {0, 2, 5}, {0, 3, 1}, {1, 0, 4}, {1,
> 1, 0}}
>
> I would have guessed there would be a way to coax Flatten to do this.
>
> Thus far I haven't found what looks like a good method.
> I have found a few awkward and bad ways but no simple clean way.
>
> Would anyone enlighten me?
>
> thanks
>

I hope you will find the following ways neither bad nor awkward!

If you are dealing only with triples (or pairs or quadruples, etc) you
could use a Flatten and Partition as in

In[1]:=
lst = {{0, 0, 0}, {{0, 0, 13}, {0, 1, 9}, {0, 2, 5},
{0, 3, 1}}, {{1, 0, 4}, {1, 1, 0}}};

In[2]:=
Partition[Flatten[lst], 3]

Out[2]=
{{0, 0, 0}, {0, 0, 13}, {0, 1, 9}, {0, 2, 5},
{0, 3, 1}, {1, 0, 4}, {1, 1, 0}}

Otherwise, you could use a replacement rule as the following

In[3]:=
lst /. x:{{__?AtomQ}..} :> Sequence @@ x

Out[3]=
{{0, 0, 0}, {0, 0, 13}, {0, 1, 9}, {0, 2, 5},
{0, 3, 1}, {1, 0, 4}, {1, 1, 0}}

Above, we are looking for non empty lists of lists of one or more atomic
expressions and we replace the head of such list by the head Sequence
(which preserves the internal structure of the main expression). Notice
that we must use a delayed ruled.

Regards,
Jean-Marc

```

• Prev by Date: Re: Any simple way to flatten all but the bottom level?
• Next by Date: Re: Any simple way to flatten all but the bottom level?
• Previous by thread: Re: Any simple way to flatten all but the bottom level?
• Next by thread: Re: Any simple way to flatten all but the bottom level?