2017-12-03 289 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的增量效果,而且可以糾正。事情是,這種方法可以在浮點時間點和整數上工作。而且快。如果它們中有很多,它會放慢速度,但是直到你達到很多數量時,它的速度都會很快。