简体   繁体   English

TypeError:'float'对象没有属性'__getitem__'

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

This code is a beginning attempt at reading a csv file into a couple of lists. 此代码是将csv文件读入几个列表的开始尝试。 I'm getting the error below, and I can't see why the float isn't being returned. 我收到下面的错误,我不明白为什么浮动没有返回。 Thanks for your help! 谢谢你的帮助!

 File "main.py", line 32, in <module>
     LR.openfile('djia_temp.csv')  
 File "main.py", line 9, in openfile
     self.xs = self.tempDiff(dataAvgandtemp)   
 File "main.py", line 18, in tempDiff
     tdArray.append([vector[0]-vector[1]]) 
 TypeError: 'float' object has no attribute '__getitem__'

Code: 码:

from processFile import processFile
import numpy as np

class processFile:

    @staticmethod
    def wholeFile(f):
        fileArray = []
        for line in f:
            fileArray.append(line.strip())
        return fileArray

    @staticmethod
    def liner(rows, columns, delimiter):
        vectors = []
        for row in rows:
            vector = []
            tok = row.split(delimiter)
            for num in columns:
                vectors.append(float(tok[num]))
        return vectors

class linRegmain:
    def openfile(self, file):
        f = open(file)
        a = processFile.wholeFile(f)[1:]
        dataAvgandtemp = processFile.liner(a, [2,3], ";")
        self.xs = self.tempDiff(dataAvgandtemp)
        self.ys = processFile.liner(a,[1], ";")
        print self.xs
        print self.ys


    def tempDiff(self, vectors):
        tdArray = []
        for vector in vectors:
            tdArray.append([vector[0]-vector[1]])
        return tdArray

if __name__ == '__main__':
    LR = linRegmain()
    LR.openfile('djia_temp.csv')

liner() claims to return a list of vectors. liner()声称返回一个向量列表。 It does not. 它不是。 You're making a list of float s: 你正在制作一个float列表:

vectors.append(float(tok[num]))

Therefore, when you call tempDiff() with the result, vector is a float , so vector[0] throws an exception. 因此,当您使用结果调用tempDiff()时, vector是一个float ,因此vector[0]会抛出异常。

I think this is what it's supposed to do: add each float to the current vector, then add the vector to the result: 我认为这是它应该做的:将每个浮点数添加到当前向量,然后将向量添加到结果:

@staticmethod
def liner(rows, columns, delimiter):
    vectors = []
    for row in rows:
        vector = []
        tok = row.split(delimiter)
        for num in columns:
            vector.append(float(tok[num])) # append to vector, not vectors
        vectors.append(vector)             # then append the vector to the result
    return vectors

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

相关问题 TypeError:“ float”对象没有属性“ __getitem__” - TypeError: 'float' object has no attribute '__getitem__' Python:TypeError:“ float”对象没有属性“ __getitem__” - Python: TypeError: 'float' object has no attribute '__getitem__' Python TypeError:“ float”对象没有属性“ __getitem__” - Python TypeError: 'float' object has no attribute '__getitem__' Python 2.7:TypeError:&#39;float&#39;对象没有属性&#39;__getitem__&#39; - Python 2.7: TypeError: 'float' object has no attribute '__getitem__' TypeError&#39;float&#39;对象在数组分配中没有属性&#39;__getitem__&#39; - TypeError 'float' object has no attribute '__getitem__' in array assignment Python-TypeError:“ float”对象没有属性“ __getitem__” - Python - TypeError: 'float' object has no attribute '__getitem__' TypeError:“浮动”对象在定义的函数中没有属性“ __getitem__” - TypeError: 'float' object has no attribute '__getitem__' in defined function TypeError:“ float”对象在函数中没有属性“ __getitem__” - TypeError: 'float' object has no attribute '__getitem__' in function 类型错误:&#39;float&#39; 对象在 Python 中没有属性 &#39;__getitem__&#39; - TypeError: 'float' object has no attribute '__getitem__' in Python TypeError:&#39;float&#39;对象在while语句中没有属性&#39;__getitem__&#39; - TypeError: 'float' object has no attribute '__getitem__' in while statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM