Gerard Flanagan wrote: > Gerard Flanagan wrote: > > John Salerno wrote: > > > If I want to create a list of the form [100, 99, 99, 98, 98, 97, 97...] > > > (where each item is repeated twice after the first one), how might I do > > > that most efficiently? > > > > series = [100] > > > > for i in range(1,10): > > series.extend([100-i]*2) > > > > print series > > > > [100, 99, 99, 98, 98, 97, 97, 96, 96, 95, 95, 94, 94, 93, 93, 92, 92, > > 91, 91] > > Alternative: > > --------------------------------------------------------- > series = [100] > > r = xrange(99,90,-1) > > for L in ( [a1,a2] for a1,a2 in zip( r, r ) ): > series.extend(L) > > print series > > out: [100, 99, 99, 98, 98, 97, 97, 96, 96, 95, 95, 94, 94, 93, 93, 92, > 92, 91, 91] > > --------------------------------------------------------- > series = ['a'] > > r = 'bcdefgh' > > for L in ( [a1,a2] for a1,a2 in zip( r, r ) ): > series.extend(L) > > print series > > out: ['a', 'b', 'b', 'c', 'c', 'd', 'd', 'e', 'e', 'f', 'f', 'g', 'g', > 'h', 'h'] > --------------------------------------------------------- >
(Nothing better to do!) def multiplier( iterable, n ): for t in zip( *[iterable] * n): yield t series1 = [100] rng = xrange(99,90,-1) for L in multiplier( rng, 2 ): series1.extend(L) series2 = ['a'] rng = 'bcdefgh' for L in multiplier( rng, 2 ): series2.extend(L) print series1 print series2 Gerard -- http://mail.python.org/mailman/listinfo/python-list