简体   繁体   English

类型错误:“类型”对象没有属性“__getitem__”

[英]TypeError: 'type' object has no attribute '__getitem__'

I keep getting an error when trying to run the rmakeprofile command.尝试运行rmakeprofile命令时,我不断收到错误rmakeprofile I get an error saying that 'type' object has no attribute '__getitem__'.我收到一条错误消息,说“类型”对象没有属性“__getitem__”。

from array import array
from ROOT import gROOT, TCanvas, TProfile, TGraph

class Data(object):
    def __init__(self, s):
        self.p = TProfile()
        self.data = []
        for line in s:
            if not line.startswith("#"):    #Removes Commented lines
                columns = line.split(',')   #Splits into Columns
                if columns:
                    datum = {
                          "threshold" : float(columns[1]),
                          "count" : float(columns[2]),
                          "rate" : float(columns[2]) /float(columns[0]),
                          "scantime" : float(columns[0])
                          }
                    self.data.append(datum)
                    print columns[1], float(columns[2])/float(columns[0])

    def rmakeprofile(self, data, xval, yval, noBins):
        self.a = array('d')
        for datum in data:
            self.a.append(float(datum[xval]))
        self.p = TProfile('p','',noBins,min(self.a),max(self.a))
        for datum in  data:
            self.p.Fill(datum[xval],datum[yval])
        return self.p

Here is the traceback:这是回溯:

p = d.rmakeprofile(data,"threshold","rate",13)

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "ray.py", line 27, in rmakeprofile
self.a = array('d')
TypeError: 'type' object has no attribute '__getitem__'

Try using the following replacements.尝试使用以下替换。

    import numpy as np
    self.a = np.asarray(['d'])

or even this also works甚至这也有效

    import numpy as np
    self.a = np.asarray('d')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM