From: Szabolcs Horvát on 26 Jul 2010 07:17 On 2010.07.25. 8:00, Michael Stern wrote: > If one has an irregularly nested list like > > {{1, 2, 3}, {1, 2, 4}, {{1, 2, 3, 4}, {1, 2, 3, 8}}, {{1, 2, 9}, {{1, 2, 13}, {4, 5, 6}}}}, > > How might one most easily transform this into a list of uniform Depth 2 ( {{1,2,3},{1,2,4},{1,2,3,4},{1,2,3,8},{1,2,9},{1,2,13},{4,5,6}} ) ? > Hi MathGroup, Here's a little trick to do this in a single step: d = {{1, 2, 3}, {1, 2, 4}, {{1, 2, 3, 4}, {1, 2, 3, 8}}, {{1, 2, 9}, {{1, 2, 13}, {4, 5, 6}}}} Level[d, {-2}] Just use the leaves of the tree as reference when taking the items, not the root. That is, use a negative value in the level specification. Cheers, Szabolcs
From: Vince Virgilio on 26 Jul 2010 06:54 On Jul 25, 2:00 am, Michael Stern <nycst...(a)gmail.com> wrote: > If one has an irregularly nested list like > > {{1, 2, 3}, {1, 2, 4}, {{1, 2, 3, 4}, {1, 2, 3, 8}}, {{1, 2, 9}, {{1, 2, 13}, {4, 5, 6}}}}, > > How might one most easily transform this into a list of uniform Depth 2 ( {{1,2,3},{1,2,4},{1,2,3,4},{1,2,3,8},{1,2,9},{1,2,13},{4,5,6}} ) ? > > Thanks in advance, > > Michael Michael, Use Level. In[1]:= irreg={{1,2,3},{1,2,4},{{1,2,3,4},{1,2,3,8}},{{1,2,9},{{1,2,13}, {4,5,6}}}}; soln={{1,2,3},{1,2,4},{1,2,3,4},{1,2,3,8},{1,2,9},{1,2,13},{4,5,6}}; uni=Level[irreg,{-2}]; uni==soln Out[4]= True Vince Virgilio
From: Peter Breitfeld on 26 Jul 2010 07:18 Michael Stern wrote: > If one has an irregularly nested list like > > {{1, 2, 3}, {1, 2, 4}, {{1, 2, 3, 4}, {1, 2, 3, 8}}, {{1, 2, 9}, {{1, > 2, 13}, {4, 5, 6}}}}, > > How might one most easily transform this into a list of uniform Depth > 2 ( {{1,2,3},{1,2,4},{1,2,3,4},{1,2,3,8},{1,2,9},{1,2,13},{4,5,6}} ) ? > > Thanks in advance, > > Michael > One possibility is to extract the list of vectors like this ll={{1, 2, 3}, {1, 2, 4}, {{1, 2, 3, 4}, {1, 2, 3, 8}}, {{1, 2, 9}, {{1, 2, 13}, {4, 5, 6}}}} uniform=Cases[ll,_?VectorQ, Infinity] Out={{1, 2, 3}, {1, 2, 4}, {1, 2, 3, 4}, {1, 2, 3, 8}, {1, 2, 9}, {1, 2, 13}, {4, 5, 6}} -- _________________________________________________________________ Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de
From: Michael Stern on 31 Jul 2010 02:42 Thanks to everybody who responded; there were many excellent answers. This one is the most jaw-dropping; even reading the documentation for Level[] I would not have realized this would work. Michael On Jul 26, 2010, at 6:38 AM, Szabolcs Horv=E1t wrote: > On 2010.07.25. 8:00, Michael Stern wrote: >> If one has an irregularly nested list like >> >> {{1, 2, 3}, {1, 2, 4}, {{1, 2, 3, 4}, {1, 2, 3, 8}}, {{1, 2, 9}, {{1, 2, 13}, {4, 5, 6}}}}, >> >> How might one most easily transform this into a list of uniform Depth 2 ( {{1,2,3},{1,2,4},{1,2,3,4},{1,2,3,8},{1,2,9},{1,2,13},{4,5,6}} ) ? >> > > > Hi MathGroup, > > Here's a little trick to do this in a single step: > > d == {{1, 2, 3}, {1, 2, 4}, {{1, 2, 3, 4}, {1, 2, 3, 8}}, {{1, 2, 9}, > {{1, 2, 13}, {4, 5, 6}}}} > > Level[d, {-2}] > > Just use the leaves of the tree as reference when taking the items, not > the root. That is, use a negative value in the level specification. > > Cheers, > Szabolcs >
First
|
Prev
|
Pages: 1 2 Prev: Documentation on (Color) Blend Next: Need to align data from sublists to Union of dates |