make elements of a list twice or more.
liuerfire Wang
liuerfire at gmail.com
Wed Aug 7 22:25:45 EDT 2013
On Thu, Aug 8, 2013 at 6:18 AM, Joshua Landau <joshua at 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 at landau.ws> wrote:
> On 7 August 2013 17:59, Peter Otten <__peter__ at 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>
***********************************/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130808/9b67e232/attachment.html>
More information about the Python-list
mailing list