MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Transforming a list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg103149] Re: Transforming a list
  • From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
  • Date: Wed, 9 Sep 2009 04:44:20 -0400 (EDT)
  • References: <h859pi$ps0$1@smc.vnet.net>

To get rid of the zeros you could use DeleteCases:

In[113]:= DeleteCases[{{c, d, e, f, g, 0}, {e, f, g, 0, 0, 0}, {g, 0,
   0, 0, 0, 0}}, 0, Infinity]

Out[113]= {{c, d, e, f, g}, {e, f, g}, {g}}

The following would do the whole trick:

Cases[lst, x_ /; Length[x] >= #][[All, #]] & /@
 Range[Max[Length /@ lst]]

as would the utterly incomprehensible version below (why do it simple
if you could make life difficult and interesting?)

Reap[
  NestWhile[
   (
     Sow[Cases[#, x_ /; x != {}][[All, 1]]];
     Cases[#, x_ /; x != {}][[All, 2 ;; -1]]
     ) &,
   lst,
   Flatten[#] != {} &
   ]
  ][[2, 1]]

Cheers -- Sjoerd

On Sep 8, 11:57 am, Don <don... at comcast.net> wrote:
> I am given: lst =  {{c, e, g}, {d, f}, {e, g}, {f}, {g}, {}}
>
> I want to form a list of sublists where the first sublist consists of the=
 first elements
> from each sublist of lst, the second sublist the second elements from eac=
h of the sublists of lst etc.
>
> The final outcome should be:
> {{c, d, e, f, g}, {e, f, g}, {g}}
>
> I can do this by first padding the sublists of lst (to make them the same=
 length)and then using Transpose. But how to get rid of the trailings 0's?
>
> For example:
>
> newLst = Map[PadRight[#, Length[lst[[1]]]] &, lst]
>
> Transpose[newLst]
>
> which produces:
>
> {{c, d, e, f, g, 0}, {e, f, g, 0, 0, 0}, {g, 0, 0, 0, 0, 0}}
>
> Is there a better way to end up with
>
> {{c, d, e, f, g}, {e, f, g}, {g}}
>
> Thank you.



  • Prev by Date: Re: Transforming a list
  • Next by Date: Re: Transforming a list
  • Previous by thread: Re: Transforming a list
  • Next by thread: FrameTicks, Thickness, TickLabels