2015-10-09 21 views
0

我有一個numpy的陣列,其中所述元件是這樣的序列,如:拆開序列

freq_chan = [ 1.00000000e-01 1.88670219e-01 -7.54695562e-03 2.62096706e-13 
    -2.13348799e-14 1.03343348e+00 -4.13380390e-02 2.62973944e-02 
    -1.05194953e-03 3.40951940e-13 -2.68000220e-14 1.44764659e+00 
    -5.79069851e-02 -1.65709468e-01 6.62852413e-03 -3.55350204e-14 
    3.45455226e-15 7.39936927e-01 -2.95981613e-02] 
[ 2.99900000e-01 1.88684850e-01 -7.54812794e-03 2.42380739e-12 
    -1.94552116e-13 1.03357295e+00 -4.13492157e-02 2.63081045e-02 
    -1.05280780e-03 2.96306692e-12 -2.36910642e-13 1.44787002e+00 
    -5.79248893e-02 -1.65738440e-01 6.63084580e-03 -4.40512728e-13 
    3.59021896e-14 7.40073244e-01 -2.96090852e-02] 

然而,序列的每個元素被認爲是主要的矩陣的元素,如

freq_chan = [ 1.00000000e-01 1.88670219e-01 -7.54695562e-03 2.62096706e-13 -2.13348799e-14 1.03343348e+00 -4.13380390e-02 2.62973944e-02 -1.05194953e-03 3.40951940e-13 -2.68000220e-14 1.44764659e+00 -5.79069851e-02 -1.65709468e-01 6.62852413e-03 -3.55350204e-14 -5.79069851e-02 -1.65709468e-01 6.62852413e-03 -3.55350204e-14 3.45455226e-15 7.39936927e-01 -2.95981613e-02] 

因此,例如,freq_chan [0,1]應返回1.88670219e-01。眼下freq_chan [0,2]返回錯誤:

print(freq_chan[0,2]) 
    TypeError: list indices must be integers, not tuple. 

這numpy的陣列從使用的代碼元組的Python列表創建:

for i,j in enumerate(freq_chan): 
    freq_chan[i] = np.array(freq_chan[i], dtype = float) 

有沒有一種方法來創建這個numpy的數組所以元組的元素被解壓縮?

感謝

+0

你肯定freq_chan是np.array? –

+0

不應該np.array(freq_chan_unfolded [i],dtype = float)使它成爲一個numpy數組 – Afzal

+0

我不是在談論freq_chan_unfolded,而是freq_chan。 –

回答

1

眼下freq_chan不是np.array,但NP陣列的列表(因此你的錯誤:你不能索引列表與元組)。在你的循環之外,你必須做freq_chan = np.array(freq_chan);那麼freq_chan將是一個2D np.array,你可以用[0,1]索引它。

請參閱的類型並不在我的改變。例如:

In[52]: a = [[ 0, 1], [2, 3]] 

In[51]: type(a) 
Out[51]: list 

In[52]: for i in range(len(a)): 
      a[i] = np.array(a[i]) 

     type(a) 
Out[52]: list 

In[53]: a[0, 1] 
Traceback (most recent call last): 

    File "<ipython-input-55-e56b75b8ad1b>", line 1, in <module> 
    a[0, 1] 

TypeError: list indices must be integers, not tuple 


In[54]: a = np.array(a) 

In[55]: a[0, 1] 
Out[55]: 1 
+0

我試着解開元素爲f = np.zeros((len(freq_chan),10))爲i,j枚舉(freq_chan): for x,y枚舉(j): f [i,x] = float(y)但它返回錯誤:f [i,x] = y 索引10超出軸1的大小10的範圍 – Afzal

+0

這是一個不同的問題。如果我的問題解決了您以前的問題,請將其標記爲已接受併發布新問題。 –

+0

@Azzal有沒有更新? –