列表索引必须是整数或切片,而不是列表

时间:2019-11-23 06:18:11

标签: python arrays

这个问题是续ofidx_rr抛出错误。我知道此错误是因为idx_rr是一个列表,它被分配为sig的索引。但是我不确定如何清除它。我可以得到一些东西吗?

N = len(sig_txt)  
R_peak = False   
j = 0
k = 0
idx_rr = [0 for l in range(N)]
idx_rr_int = 0
sig = [0 for m in range(N)]
sig_int = 0
#print(len(sig_txt))
for i in range (len(sig_txt)):
    if (sig_txt[i] == 32767):
        idx_rr[k] = np.array(i)
        sig[j] = np.array(sig_txt[i+1])
        k = k + 1
        j = j + 1
        R_peak = True
    else:
        if (R_peak == False):
            sig_int = i
            #print(j)
            #print(sig_txt[i])
            sig[j] = np.array(sig_txt[i])
            j = j + 1
        else:
            R_peak = False

#print(idx_rr)
#print(sig)
plt.figure(figsize=(20,8))
plt.plot(sig)
plt.scatter([idx_rr], [sig[idx_rr]], c='g')  
plt.show() 


TypeError Traceback (most recent call last)
<ipython-input-387-dcea2717b9f2> in <module>
----> 1 get_intervals('/home/yasaswini/hp2-notebooks/ecg_data/Recorded_Data_Patch_Simulator  
/TXT_Files/ECG_data_128Hz_Simulator_Patch_Normal_data.txt',128)

<ipython-input-384-606e74089dfb> in get_intervals(fname, sampling_rate)
     38     plt.figure(figsize=(20,8))
     39     plt.plot(sig)
     ---> 40     plt.scatter([idx_rr], [sig[idx_rr]], c='g')
     41     plt.show()
     42 

TypeError: list indices must be integers or slices, not list

2 个答案:

答案 0 :(得分:1)

@goodvibration,我明白你的意思。这是我的最终代码。我得到了预期的结果。谢谢您的指点。

def ECG_marker_remove(sig_txt):
N = len(sig_txt)  
j = 0
k = 0
idx_rr = np.zeros(N, dtype=np.int32)
sig = np.zeros(N)
for i in range (N):
    if (sig_txt[i] == 32767):
        if (j == N-1):
            sig[j] = 0
            idx_rr[k] = 0
            j = j + 1
            k = k + 1
        else:
            sig[j] = sig_txt[i+1]
            idx_rr[k] = i
            j = j + 1
            k = k + 1
    else:
        sig[j] = sig_txt[i]
        j = j + 1
idx_rr_peaks = idx_rr[:k]
return sig, idx_rr_peaks

答案 1 :(得分:0)

茉莉花,这行得通,尽管我认为您仍然需要进行一些更改:

N = len(sig_txt)  
R_peak = False   
j = 0
k = 0
idx_rr = [0 for l in range(N)]
idx_rr_int = 0
sig = [0 for m in range(N)]
sig_int = 0
#print(len(sig_txt))
for i in range (len(sig_txt)):
    if (sig_txt[i] == 32767):
        idx_rr[k] = np.array(i)
        sig[j] = sig_txt[i+1]
        k = k + 1
        j = j + 1
        R_peak = True
    else:
        if (R_peak == False):
            sig_int = i
            #print(j)
            #print(sig_txt[i])
            sig[j] = sig_txt[i]
            j = j + 1
        else:
            R_peak = False

#print(idx_rr)
#print(sig)
plt.figure(figsize=(20,8))
plt.plot(sig)
plt.scatter(idx_rr, sig, c='g')  
plt.show()