[英]Generate list with new items that contains more than one element
I have an array a
. 我有一个数组
a
。 I want to create new array with doubled size where items is x*2 and x*3. 我想创建两倍大小的新数组,其中项目是x * 2和x * 3。
For example: a = [1,10,100]
result must be b = [2,3,20,30,200,300]
例如:
a = [1,10,100]
结果必须为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], [])
我知道这种方式(丑陋且非常慢):
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. 似乎胜过所有答案,但是当
a
很大时,就会输给numpy
解决方案。
You can perform the multiplications in a list comprehension, then zip
and flatten the resulting lists. 您可以对列表进行乘法运算,然后
zip
并展平结果列表。
>>> 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):
以下是其中之一,使用numpy(第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. 但这是因为
a
小。 When it's larger, numpy becomes much better. 当它更大时,numpy变得更好。
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: 包含@CoryKramer的上述答案的timeit结果,这对于小型数组最快,但对于大型数组却会变得麻木:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.