Jasmine
Jasmine

Reputation: 476

TypeError: 'int' object does not support item assignment error

sig_txt: [45 67 83 32767 101 90 50]

idx_rr:[101] // index after 32767

sig: [45 67 83 101 90 50] // all the elements except 32767

Basically what happens is from the original array, all except the element whose value is equal to 32767 is passed to the sig array. And idx_rr gets the value immediate after 32767. I have written a python for the same. But I am getting this error:

"'int' object does not support item assignment"

when I am trying to plot the idx_rr value of sig. Can I get some help on this.

R_peak = False   
j = 0
k = 0
idx_rr = []
sig = []

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

        else:
            R_peak = False

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-1474-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-1471-5a6b384defd1> in get_intervals(fname,sampling_rate)
22             if (R_peak == False):
23                 sig = i
---> 24                 sig[k] = np.array(sig_txt[i])
25                 k = k + 1
26 

TypeError: 'int' object does not support item assignment

Upvotes: 0

Views: 398

Answers (2)

po.pe
po.pe

Reputation: 1162

With idx_rr = i+1 the formerly defined list becomes an int so there's no index idx_rr[j] to assign a value to. The same applies to the sig variable.

Note that the problem is not the declaration, python allows you to re-assign a variable to a new type. The problem is that idx_rr is of type int and you cannot access and index idx_rr[j] of an int.

Try this

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

        else:
            R_peak = False

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

Upvotes: 1

squareRoot17
squareRoot17

Reputation: 890

After the line idx__rr = i+1, you are changing the data type of idx_rr, which was list previously, to int.

Change the variable name of the list to get rid of the error.

Upvotes: 0

Related Questions