Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!eweka.nl!hq-usenetpeers.eweka.nl!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'only,': 0.07; 'wang': 0.07; "(i'd": 0.09; '[1,': 0.09; '__name__': 0.09; 'items)': 0.09; 'len(x)': 0.09; 'tmp': 0.09; 'yeah,': 0.09; 'python': 0.11; 'def': 0.12; '2.7': 0.14; '"from': 0.16; '>>': 0.16; "'__main__':": 0.16; '[none]': 0.16; 'item)': 0.16; 'itertools': 0.16; 'skip:7 20': 0.16; 'subject:make': 0.16; 'url:peps': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'seems': 0.21; 'import': 0.22; 'aug': 0.22; 'print': 0.22; 'skip:l 30': 0.24; 'url:dev': 0.24; '>': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'am,': 0.29; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'posting': 0.31; 'twitter:': 0.31; '>>>>': 0.31; 'fast.': 0.31; 'skip:7 10': 0.31; 'lists': 0.32; 'url:python': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'url:listinfo': 0.36; 'url:org': 0.36; 'should': 0.36; 'skip:4 10': 0.37; 'list': 0.37; 'skip:& 10': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'august': 0.61; 'skip:t 30': 0.61; 'new': 0.61; 'more': 0.64; 'different': 0.65; 'regards.': 0.65; 'here': 0.66; 'beautiful': 0.68; 'repeat': 0.74; 'charset:gb2312': 0.80; 'beats': 0.84; 'otten': 0.84; 'skip:/ 30': 0.84; 'skip:\xb5 60': 0.84; 'url:tt': 0.84; 'do:': 0.91; 'skip:4 20': 0.91; 'write:': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=f++4IBm1zQsrMLHUk1cqo2U3I8fg6HEVK+bCjRcYyt8=; b=AUIbKjkh/p6m6KHFNhK+75+UcvDhJTI8lEKJaqXoIkBv3E/B6pbDK8km2ttEXnjSh/ AEEcNmxaQ/JPbhCXGftt9wUg1O6UqtxoS69Bk++ABj7SbEDMKEwxk0h6dKoXxxAowvaV +LRi+NHJEgKlSVtgCMpugmPFb9C66KE5sZG8zcf5S5obS6w7DbjYZRx7HenW0cP1qWcO LxMish/sQ4e+c/+43Q2pzQxNHJ0Dht7Kmpayf18OAy3UWKsUqe7T0lISuUxFDsmysLQ3 W5LcngBph0VlbikYbOya13U6Ggo6grl7Kmfy1CJa8rK03ypqVzrO17XjFD7fXqHMV7OI V9/g== MIME-Version: 1.0 X-Received: by 10.52.64.179 with SMTP id p19mr1645370vds.92.1375928745937; Wed, 07 Aug 2013 19:25:45 -0700 (PDT) In-Reply-To: References: Date: Thu, 8 Aug 2013 10:25:45 +0800 Subject: Re: make elements of a list twice or more. From: liuerfire Wang To: python-list@python.org Content-Type: multipart/alternative; boundary=20cf307ac79f2353f504e366601d X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 655 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375928755 news.xs4all.nl 15868 [2001:888:2000:d::a6]:53291 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52159 --20cf307ac79f2353f504e366601d Content-Type: text/plain; charset=GB2312 Content-Transfer-Encoding: quoted-printable On Thu, Aug 8, 2013 at 6:18 AM, Joshua Landau wrote: > > I'm actually posting to point out > http://www.python.org/dev/peps/pep-0448/ would let you write: > > [*(item, item) for item in items] It seems like that it can be only used in python 3.4? I just use python 2.7 because of work needs. > > > For lists only, should be fast: > > > >>>> result =3D 2*len(items)*[None] > >>>> result[::2] =3D result[1::2] =3D items > >>>> result > > [b, b, a, a, c, c] Yeah, this is amazing and very fast. I just make a test: import timeit from itertools import chain, tee, repeat x =3D [1, 2, 3, 4, 5, 6, 7, 8] def test1(): [i for i in x for y in range(2)] def test2(): tmp =3D [] for i in x: tmp.append(i) tmp.append(i) def test3(): list(chain.from_iterable(zip(*tee(x)))) def test4(): result =3D 2 * len(x) * [None] result[::2] =3D result[1::2] =3D x if __name__ =3D=3D '__main__': t1 =3D timeit.Timer("test1()", "from __main__ import test1") t2 =3D timeit.Timer("test2()", "from __main__ import test2") t3 =3D timeit.Timer("test3()", "from __main__ import test3") t4 =3D timeit.Timer("test4()", "from __main__ import test4") print t1.timeit(1000000) print t2.timeit(1000000) print t3.timeit(1000000) print t4.timeit(1000000) And the result is: 4.56177520752 2.85114097595 7.61084198952 1.29519414902 On Thu, Aug 8, 2013 at 6:18 AM, Joshua Landau wrote: > On 7 August 2013 17:59, Peter Otten <__peter__@web.de> wrote: > > liuerfire Wang wrote: > > > >> Here is a list x =3D [b, a, c] (a, b, c are elements of x. Each of the= m > are > >> different type). Now I wanna generate a new list as [b, b, a, a, c, c= ]. > >> > >> I know we can do like that: > >> > >> tmp =3D [] > >> for i in x: > >> tmp.append(i) > >> tmp.append(i) > >> > >> However, I wander is there a more beautiful way to do it, like [i for = i > in > >> x]? > > > > Using itertools: > > > >>>> items > > [b, a, c] > >>>> from itertools import chain, tee, repeat > > > >>>> list(chain.from_iterable(zip(*tee(items)))) > > [b, b, a, a, c, c] > > > > Also using itertools: > > > >>>> list(chain.from_iterable(repeat(item, 2) for item in items)) > > [b, b, a, a, c, c] > > list(chain.from_iterable([item, item] for item in items)) > ? > > > I'm actually posting to point out > http://www.python.org/dev/peps/pep-0448/ would let you write: > > [*(item, item) for item in items] > > which I think is totz rad and beats out OP's > > [item for item in items for _ in range(2)] > > in readability, succinctness and obviousness. > > > PS: For jokes, you can also painfully do: > > list((yield item) or item for item in items) > > > > For lists only, should be fast: > > > >>>> result =3D 2*len(items)*[None] > >>>> result[::2] =3D result[1::2] =3D items > >>>> result > > [b, b, a, a, c, c] > > > > But I would call none of these beautiful... > > Au contraire, that is marvelous (I'd still avoid it, though). > -- > http://mail.python.org/mailman/listinfo/python-list > --=20 Best regards. /********************************** google+: +liuerfire twitter: @liuerfire =B5=B0=CC=DB=B2=BB=B5=B0=CC=DB=B5=C4=B6=BC=BF=C9=D2=D4=CA=D4=D7=C5=B5=E3=D2= =BB=CF=C2~^_^~ ***********************************/ --20cf307ac79f2353f504e366601d Content-Type: text/html; charset=GB2312 Content-Transfer-Encoding: quoted-printable
On Thu, Aug 8, 2013 at 6:18 AM, Joshua Landau <joshua@l= andau.ws> wrote:

I'm actually posting to point out
http://www.python.org/dev/peps/pep= -0448/ would let you write:

    [*(item, item) fo= r item in items]

It seems like that it can be only used in python = 3.4? I just use python 2.7 because of work needs.
 

> For lists only, should be fast:
>
>>>> result= =3D 2*len(items)*[None]
>>>> result[::2] =3D result[1::2] = =3D items
>>>> result
> [b, b, a, a, c, c]
 
Yeah, this is amazing and very fast.

<= /div>
I just make a test:

import timeit

from  import chain,<= /span> tee, repeat=
 
=3D [1, 2, 3, 4<= /span>, 5,&= nbsp;6, <= span style=3D"font-family:monospace;line-height:1.2em;background-color:rgb(= 247,247,247);color:rgb(255,69,0)">7, 8]
 
def=  test1():
    [for i in x for y in range
(2)]
 
def=  test2():

    tmp =3D[]
    for i in x:
        tmp.<= span style=3D"line-height:1.2em;background-color:rgb(247,247,247);font-fami= ly:monospace">append(i)
        tmp.<= span style=3D"line-height:1.2em;background-color:rgb(251,251,251);font-fami= ly:monospace">append(i)
 
def test3():

    list(chain.from_iterable(zip(*tee(x))))
 
def=  test4():
    result =3D 2 * len<= /span>(x) * = [None]
    result[::2] =3D result[1
::= 2] 
=3D x
 
if&= nbsp;__name__ =3D=3D '__main__':
    t1 =3D&nb= sp;timeit.Timer("test1()", = ;"from __main__ import test= 1")
    t2 =3D&nb= sp;timeit.Timer("test2()", = ;"from __main__ import test= 2")
    t3 =3D&nb= sp;timeit.Timer("test3()", = ;"from __main__ import test= 3")
    t4 =3D&nb= sp;timeit.Timer("test4()", = ;"from __main__ import test= 4")
    print t1.timeit(1000000)
    print t2.timeit(1000000)
    print t3.timeit(1000000)
    print t4.<= /span>timeit(1000000)

And the result is:
4.56177520752
2.85114097595
7.61084198952
1.29519414902
=


On Thu, Aug 8= , 2013 at 6:18 AM, Joshua Landau <joshua@landau.ws> wrote:
On 7 August 2013 17:59, Pe= ter Otten <__peter__@web.de> wr= ote:
> liuerfire Wang wrote:
>
>> Here is a list x =3D [b, a, c] (a, b, c are elements of x. Each of= them are
>> different type).  Now I wanna generate a new list as [b, b, a= , a, c, c].
>>
>> I know we can do like that:
>>
>> tmp =3D []
>> for i in x:
>>     tmp.append(i)
>>     tmp.append(i)
>>
>> However, I wander is there a more beautiful way to do it, like [i = for i in
>> x]?
>
> Using itertools:
>
>>>> items
> [b, a, c]
>>>> from itertools import chain, tee, repeat
>
>>>> list(chain.from_iterable(zip(*tee(items))))
> [b, b, a, a, c, c]
>
> Also using itertools:
>
>>>> list(chain.from_iterable(repeat(item, 2) for item in items= ))
> [b, b, a, a, c, c]

list(chain.from_iterable([item, item] for item in items))
?


I'm actually posting to point out
http= ://www.python.org/dev/peps/pep-0448/ would let you write:

    [*(item, item) for item in items]

which I think is totz rad and beats out OP's

    [item for item in items for _ in range(2)]

in readability, succinctness and obviousness.


PS: For jokes, you can also painfully do:

    list((yield item) or item for item in items)


> For lists only, should be fast:
>
>>>> result =3D 2*len(items)*[None]
>>>> result[::2] =3D result[1::2] =3D items
>>>> result
> [b, b, a, a, c, c]
>
> But I would call none of these beautiful...

Au contraire, that is marvelous (I'd still avoid it, though).
--
http://mail.python.org/mailman/listinfo/python-list



--
Best regards.
/**********************************
google+: +liuerfire twitter: @liuerfire
***********************************/

--20cf307ac79f2353f504e366601d--