简体   繁体   中英

Generate list with new items that contains more than one element

I have an array a . I want to create new array with doubled size where items is x*2 and x*3.

For example: a = [1,10,100] result must be b = [2,3,20,30,200,300]

I know this (ugly and very slow) way: b = sum([[x*2,x*3] for x in a], [])

There is other way (truly I want shortest way :)?

This can be done using a list comprehension with nested loops

In [4]: [y for x in a for y in (x * 2, x * 3)]
Out[4]: [2, 3, 20, 30, 200, 300]

Seems to outperform all answers, but loses to the numpy solution when a is large.

You can perform the multiplications in a list comprehension, then zip and flatten the resulting lists.

>>> a = [1,10,100]
>>> b = [j for i in zip([i*2 for i in a], [i*3 for i in a]) for j in i]
>>> b
[2, 3, 20, 30, 200, 300]

You can do this several ways. Below is one of them, using numpy (line 4):

In [1]: import numpy as np

In [2]: a = [1, 10, 100]

In [3]: %timeit sum([[x*2,x*3] for x in a], [])
1000000 loops, best of 3: 632 ns per loop

In [4]: %timeit x = np.array(a); np.array([x*2,x*3]).T.ravel()
100000 loops, best of 3: 3.25 µs per loop

Your way is faster! But this is because a is small. When it's larger, numpy becomes much better.

In [5]: a = range(1000)

In [6]: %timeit sum([[x*2,x*3] for x in a], [])
100 loops, best of 3: 2.37 ms per loop

In [7]: %timeit x = np.array(a); np.array([x*2,x*3]).T.ravel()
10000 loops, best of 3: 39.6 µs per loop

Included timeit results for @CoryKramer's answer above, which is fastest for small arrays but also loses to numpy for large arrays:

In [10]: a = [1, 10, 100]

In [11]: %timeit [j for i in zip([i*2 for i in a], [i*3 for i in a]) for j in i]
1000000 loops, best of 3: 853 ns per loop

In [12]: a = range(1000)

In [13]: %timeit [j for i in zip([i*2 for i in a], [i*3 for i in a]) for j in i]
1000 loops, best of 3: 252 µs per loop

Generally using tuples are faster than list:

>>> timeit.timeit("sum([[x*2,x*3] for x in (1,10,100)], [])", number=10000)
0.023060083389282227
>>> timeit.timeit("sum(((x*2,x*3) for x in (1,10,100)), ())", number=10000)
0.01667189598083496

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM