Re: Transforming a list
- To: mathgroup at smc.vnet.net
- Subject: [mg103146] Re: [mg103108] Transforming a list
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Wed, 9 Sep 2009 04:43:48 -0400 (EDT)
- References: <200909080958.FAA26578@smc.vnet.net>
- Reply-to: drmajorbob at yahoo.com
lst = {{c, e, g}, {d, f}, {e, g}, {f}, {g}, {}}; max = Max[Length /@ lst]; newLst = Transpose[PadRight[#, max, Null] & /@ lst] /. Null :> Sequence[] {{c, d, e, f, g}, {e, f, g}, {g}} or newLst = Transpose[ PadRight[#, max, Null] & /@ lst] /. {notNull__, Null ..} :> {notNull} {{c, d, e, f, g}, {e, f, g}, {g}} or newLst = Replace[ Transpose[ PadRight[#, max, Null] & /@ lst], {notNull__, Null ..} :> {notNull}, 1] {{c, d, e, f, g}, {e, f, g}, {g}} The last method may be faster if the Transpose has MANY rows or LONG ones, since it looks at each row only once, and all at once (I think). Bobby On Tue, 08 Sep 2009 04:58:16 -0500, Don <donabc 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 > 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}} > > Thank you. > -- DrMajorBob at yahoo.com
- References:
- Transforming a list
- From: Don <donabc@comcast.net>
- Transforming a list