TypeError:“ int”对象不支持项目分配错误 [英] TypeError: 'int' object does not support item assignment error

查看:114
本文介绍了TypeError:“ int”对象不支持项目分配错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sig_txt:[45 67 83 32767 101 90 50]

idx_rr: [101] //在32767之后的索引

信号:[45 67 83 101 90 50] //所有除32767

基本上发生的是来自原始数组,除值等于 32767 的元素以外的所有元素>传递给 sig 数组。而 idx_rr 会在 32767 之后立即获取值。我已经写了同样的python。但是我遇到了这个错误:

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'对象不支持项目分配

"'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


推荐答案

使用 idx_rr = i + 1 以前定义的列表成为 int ,因此没有索引 idx_rr [j] 为其分配值。 sig 变量也是如此。

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.

请注意,问题不在于声明,python允许您重新输入-将变量分配给新类型。问题是 idx_rr 的类型为 int ,并且您无法访问和索引 idx_rr [j ] int

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.

尝试一下

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() 

这篇关于TypeError:“ int”对象不支持项目分配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆