MathGroup Archive 2009

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

Search the Archive

Re: Transforming a list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg103155] Re: Transforming a list
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Wed, 9 Sep 2009 04:45:26 -0400 (EDT)

On 9/8/09 at 5:58 AM, donabc at comcast.net (Don) 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 each 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}}

One way to do this would be:

In[3]:= list = {{c, e, g}, {d, f}, {e, g}, {f}, {g}, {}};
Table[Cases[list, _?(Length[#] > n - 1 &)][[All, n]], {n,
   Max[Length /@ list]}]

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

Note, you can get the same result by adding one step to the
process you've outlined above. That is:

In[5]:= DeleteCases[#, 0] & /@
  Transpose[Map[PadRight[#, Length[list[[1]]]] &, list]]

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



  • Prev by Date: Re: Transforming a list
  • Next by Date: Re: Re: Re: Manipulate: How to
  • Previous by thread: Re: Transforming a list
  • Next by thread: Re: Transforming a list