Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #52159
| 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 | <liuerfire@gmail.com> |
| 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 | <CAN1F8qXrT1ArBbP3vJVfTvhePeAxH=KO1aO+TNnq5Hxpyt2LtQ@mail.gmail.com> |
| References | <CAFysF+Sh=1_K9++GELiGjYe8t-T53m0_6H=5FZyL8XEer8t8XA@mail.gmail.com> <kttue3$lsq$1@ger.gmane.org> <CAN1F8qXrT1ArBbP3vJVfTvhePeAxH=KO1aO+TNnq5Hxpyt2LtQ@mail.gmail.com> |
| Date | Thu, 8 Aug 2013 10:25:45 +0800 |
| Subject | Re: make elements of a list twice or more. |
| From | liuerfire Wang <liuerfire@gmail.com> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.334.1375928755.1251.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
On Thu, Aug 8, 2013 at 6:18 AM, Joshua Landau <joshua@landau.ws> 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 = 2*len(items)*[None]
> >>>> result[::2] = result[1::2] = 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 = [1, 2, 3, 4, 5, 6, 7, 8]
def test1():
[i for i in x for y in range(2)]
def test2():
tmp = []
for i in x:
tmp.append(i)
tmp.append(i)
def test3():
list(chain.from_iterable(zip(*tee(x))))
def test4():
result = 2 * len(x) * [None]
result[::2] = result[1::2] = x
if __name__ == '__main__':
t1 = timeit.Timer("test1()", "from __main__ import test1")
t2 = timeit.Timer("test2()", "from __main__ import test2")
t3 = timeit.Timer("test3()", "from __main__ import test3")
t4 = 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 <joshua@landau.ws> wrote:
> On 7 August 2013 17:59, Peter Otten <__peter__@web.de> wrote:
> > liuerfire Wang wrote:
> >
> >> Here is a list x = [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 = []
> >> 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 = 2*len(items)*[None]
> >>>> result[::2] = result[1::2] = 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 <http://gplus.to/onepiece> twitter:
@liuerfire<https://twitter.com/#!/liuerfire>
蛋疼不蛋疼的都可以试着点一下~^_^~ <http://db.tt/YGEdRM0>
***********************************/
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: make elements of a list twice or more. liuerfire Wang <liuerfire@gmail.com> - 2013-08-08 10:25 +0800
csiph-web