2017-12-03 290 views
0

我有号码的清单:展开数字在列表

[10,20,30] 

我需要的是要根据预定义的增量展开。因此,让我们把x增量和x=2,我的结果应该是:

[10,12,14,16,18,20,22,24,.....,38] 

现在我使用for循环,但它是非常缓慢的,我想知道是否有一种更快的方式。

编辑:

newA = [] 
for n in array: 
    newA= newA+ generateNewNumbers(n, p, t) 

功能生成新的号码简单地生成新的号码添加到列表中。

EDIT2: 为了更好地定义问题的第一阵列包含一些时间戳:

[10,20,30] 

我有两个参数,一个是采样率,一个是采样时间,我需要的是扩大数组根据采样率在两个时间戳之间添加正确数量的时间戳。 举例来说,如果我有一个采样率3和采样时间3的结果应该是:

[10,13,16,19,20,23,26,29,30,33,36,39] 
+1

显示你的工作代码?这个问题从样本中不太清楚,似乎有太多的假设。 – Divakar

+0

你能发布你的代码吗? –

+0

为什么它应该停止在38,任何条件为 – csharpcoder

回答

3

您可以使用np.add.outer将相同的增量集合添加到每个时间戳,然后使用ravel将结果展平。

import numpy as np 
a = [10,20,35] 
inc = 3 
ninc = 4 
np.add.outer(a, inc * np.arange(ninc)).ravel() 
# array([10, 13, 16, 19, 20, 23, 26, 29, 35, 38, 41, 44]) 
1

您可以使用列表comprhensions,但我不知道我的理解对最后一点列入停止条件

a = [10, 20, 30, 40] 
t = 3 
sum([[x for x in range(y, z, t)] for y, z in zip(a[:-1], a[1:])], []) + [a[-1]] 

会给

[10, 13, 16, 19, 20, 23, 26, 29, 30, 33, 36, 39, 40] 
+0

还有两件事:还有40件应该扩展到40,43,46,49,我需要将结果保存在一个新的数组中,保持输入不变。谢谢。 @percusse –

+2

@GuidoMuscioni扩展到什么数字?我如何从'a'确定50个?我应该假设周期?这不会通过将其分配给变量的方式修改输入。 – percusse

+0

这种方法不起作用,实际上我不知道为什么,但是它产生的数组比他们应该多。你可以看到B.M的答案。更好地理解我想要的东西。 @percusse –

1

使用rangeitertools.chain

l = [10,20,30] 
x = 3 
from itertools import chain 
list(chain(*[range(i,i+10,x) for i in l])) 
#Output: 
#[10, 13, 16, 19, 20, 23, 26, 29, 30, 33, 36, 39] 
+0

这将返回输入数组l –

0

这里是一堆很好的答案了。但我会建议numpy和线性插值。

# Now, this will give you the desired result with your first specifications 
# And in pure Python too 
t = [10, 20, 30] 
increment = 2 
last = int(round(t[-1]+((t[-1]-t[-2])/float(increment))-1)) # Value of last number in array 
# Note if you insist on mathematically "incorrect" endpoint, do: 
#last = ((t[-1]+(t[-1]-t[-2])) -((t[-1]-t[-2])/float(increment)))+1 
newt = range(t[0], last+1, increment) 
# And, of course, this may skip entered values (increment = 3 

# But what you should do instead, when you use the samplerate is 
# to use linear interpolation 
# If you resample the original signal, 
# Then you resample the time too 
# And don't expand over the existing time 
# Because the time doesn't change if you resampled the original properly 
# You only get more or less samples at different time points 
# But it lasts the same length of time. 
# If you do what you originally meant, you actually shift your datapoints in time 
# Which is wrong. 
import numpy 

t = [10, 20, 30, 40, 50, 60] 
oldfs = 4000 # 4 KHz samplerate 
newfs = 8000 # 8 KHz sample rate (2 times bigger signal and its time axis) 
ratio = max(oldfs*1.0, newfs*1.0)/min(newfs, oldfs) 
newlen = round(len(t)*ratio) 
numpy.interp(
    numpy.linspace(0.0, 1.0, newlen), 
    numpy.linspace(0.0, 1.0, len(t)), 
    t) 

此代码也可以重新采样您的原始信号(如果有的话)。如果你只是想在更多的时间点之间塞进,你也可以使用插值。再次,不要超越现有的时间。虽然这段代码实现了它,但与第一个代码兼容。这样你就可以得到你可以做什么的想法。

t = [10, 20, 30] 
increment = 2 
last = t[-1]+((t[-1]-t[-2])/float(increment))-1 # Value of last number in array 
t.append(last) 
newlen = (t[-1]-t[0])/float(increment)+1 # How many samples we will get in the end 
ratio = newlen/len(t) 
numpy.interp(
    numpy.linspace(0.0, 1.0, newlen), 
    numpy.linspace(0.0, 1.0, len(t)), 
    t) 

这虽然是2.5而不是2的增量效果,而且可以纠正。事情是,这种方法可以在浮点时间点和整数上工作。而且快。如果它们中有很多,它会放慢速度,但是直到你达到很多数量时,它的速度都会很快。