On Tuesday, October 28, 2014 6:56:10 AM UTC+5:30, Chris Angelico wrote: > On Tue, Oct 28, 2014 at 12:12 PM, Rustom Mody wrote: > > More generally for d being a 2-D reshape of dd (which may be anything > > as long as the size matches) > > > >>>> dd = [1,2,3,4,5,6,7,8,9,10,11,12] > >>>> d=[[dd[i*3+j] for j in range(3)] for i in range(4)] > >>>> d > > [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] > > The inner comprehension should surely be a slice: > > >>> [dd[i*3:i*3+3] for i in range(4)] > [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
Sweet! [Something for my class today] Looks even better this way: >>> dd = range(1,13) >>> [dd[i*3:i*3+3] for i in range(4)] [range(1, 4), range(4, 7), range(7, 10), range(10, 13)] >>> In the same vein a transpose: >>> [list(dd[i::3]) for i in range(3)] [[0, 3, 6, 9], [1, 4, 7, 10], [2, 5, 8, 11]] [Not sure why both are 3; no 4's...] -- https://mail.python.org/mailman/listinfo/python-list