how to duplicate array entries
Chris Rebert
clp2 at rebertia.com
Mon Jan 11 01:30:04 EST 2010
On Sun, Jan 10, 2010 at 10:21 PM, Sebastian <sebastian.langer at gmx.de> wrote:
> Hi there,
>
> I have an array x=[1,2,3]
>
> Is there an operator which I can use to get the result
> [1,1,1,2,2,2,3,3,3] ?
>
> I tried x*3, which resulted in [1,2,3,1,2,3,1,2,3]
> I also tried [[b,b,b] for b in x] which led to [[1,2,3],[1,2,3],
> [1,2,3]], but this isn't what I want either.
from itertools import chain, repeat
n = 3
stretched = list(chain(*[repeat(item, n) for item in x]))
Cheers,
Chris
--
http://blog.rebertia.com
More information about the Python-list
mailing list