Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'int' object has no attribute '__getitem__'

import math
import os


class collection:
    col = [[0 for col in range(5)] for row in range(6)]
    dist = [[0 for col in range(6)] for row in range(6)]
    filename = ""
    result = ""

    def __init__(self,arg1):
        self.filename = arg1

    def coll(self):

        for i in range(6):
            try:
                if(i==0):
                    f = open(self.filename,'r')
                elif(i==1):
                    f = open("chap1.txt",'r')
                elif(i==2):
                    f = open("chap2.txt",'r')
                elif(i==3):
                    f = open("chap3.txt",'r')
                elif(i==4):
                    f = open("chap4.txt",'r')
                elif(i==5):
                    f = open("chap5.txt",'r')

                for j in range(5):
                    self.result = f.readline()
                    self.col[i][j] = self.result
            finally:
                print "file handling error"

    def distance(self):
        for i in range[6]:
            for j in range[6]:
                dis = 0
                for k in range[5]:
                    dis += math.fabs((self.col[i][k]-self.col[j][k])*(j-i))
                self.dist[i][j] = dis
                self.dist[i][i] = sys.maxdouble
        return self.dist

class profile:
    dist = [[0 for col in range(6)]for row in range(6)]
    filename = ""
    pque = [[0 for col in range(6)]for row in range(6)]
    d = [[0 for col in range(6)]for row in range(6)]
    par = [[0 for col in range(6)]for row in range(6)]
    st = 0

    def __init__(self,arg1):
        self.filename = arg1

    def begin(self):
        ob = collection(self.filename)
        ob.coll()
        dist = ob.distance()

    def sssp(self):
        for i in range(6):
            pque[i] = sys.maxdouble
            d[i] = sys.maxdouble
        d[0] = 0
        pque[0] = 0

        while isempty()==0:
            u = extract_min()
            for i in range(6):
                if d[i]>d[u]+dist[u][i]:
                   d[i] = d[u]+dist[u][i]
                   pque_deckey(i,d[i])
                   par[i]=u
                if u!=0:
                    print u
            print "\n"
            for i in range(6):
                print par[i]

    def extract_min():
        ret = 0
        shift = 0
        minimum = pque[0]

        for i in range(6):
            if pque[i]<minimum:
                minimum = pque[i]
                ret = i
        pque[ret] = sys.maxdouble
        return ret

    def isempty(self):
        count = 0
        for i in range(6):
            if pque[i] == sys.maxdouble:
                count=count+1
        if count==6:
            return 1
        else :
            return 0

    def pque_deckey(self,im,di):
        pque[im]=di

class main:
    filename = raw_input("enter name of student:\n")
    filename = filename + ".txt"
    if(os.path.exists(filename)==1):
        f = file(filename,"r")
    else:
        f = file(filename,"w+")
        att1 = raw_input("att1 score:\n")
        att2 = raw_input("att2 score:\n")
        att3 = raw_input("att3 score:\n")
        att4 = raw_input("att4 score:\n")
        att5 = raw_input("att5 score:\n")
        f.write(att1)
        f.write("\n")
        f.write(att2)
        f.write("\n")
        f.write(att3)
        f.write("\n")
        f.write(att4)
        f.write("\n")
        f.write(att5)
        f.write("\n")
    stud = profile(filename)
    stud.begin()
    stud.sssp()

it shows a runtime error :

File "C:\Python27\winculum.py", line 33, in coll
    self.col[i][j] = self.result
TypeError: 'int' object has no attribute '__getitem__'

I am just a beginner at python, and I am unable to rectify this even after searching on the net.

like image 453
here_to_learn Avatar asked Jun 25 '12 17:06

here_to_learn


People also ask

What is int object has no attribute?

The Python "AttributeError: 'int' object has no attribute" occurs when we try to access an attribute that doesn't exist on an integer. To solve the error, make sure the value is of the expected type before accessing the attribute.

How do I fix TypeError int object is not callable?

But in Python, this would lead to the Typeerror: int object is not callable error. To fix this error, you need to let Python know you want to multiply the number outside the parentheses with the sum of the numbers inside the parentheses. Python allows you to specify any arithmetic sign before the opening parenthesis.

What does int object is not Subscriptable?

Python TypeError: 'int' object is not subscriptableThis error occurs when you try to use the integer type value as an array. In simple terms, this error occurs when your program has a variable that is treated as an array by your function, but actually, that variable is an integer.

Is not iterable Python?

The Python "TypeError: 'type' object is not iterable" occurs when we try to iterate over a class that is not iterable, e.g. forget to call the range() function. To solve the error, make the class iterable by implementing the __iter__() method. Here is an example of how the error occurs when the range function.


3 Answers

The error:

'int' object has no attribute '__getitem__'

means that you're attempting to apply the index operator [] on an int, not a list. So is col not a list, even when it should be? Let's start from that.

Look here:

col = [[0 for col in range(5)] for row in range(6)]

Use a different variable name inside, looks like the list comprehension overwrites the col variable during iteration. (Not during the iteration when you set col, but during the following ones.)

like image 87
Kos Avatar answered Oct 17 '22 13:10

Kos


This error could be an indication that variable with the same name has been used in your code earlier, but for other purposes. Possibly, a variable has been given a name that coincides with the existing function used later in the code.

like image 31
mel Avatar answered Oct 17 '22 11:10

mel


Some of the problems:

for i in range[6]:
            for j in range[6]:

should be:

range(6)
like image 3
Geo Avatar answered Oct 17 '22 12:10

Geo