11 Replies - 11716 Views - Last Post: 02 November 2011 - 02:48 PM Rate Topic: -----

#1 Ruru123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 09-October 11

not subscriptable?

Posted 02 November 2011 - 10:31 AM

Hey

what does:

TypeError: 'int' object is not subscriptable mean..???

P[i][j] = k



that was part of my code that had the error
Is This A Good Question/Topic? 0
  • +

Replies To: not subscriptable?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: not subscriptable?

Posted 02 November 2011 - 10:50 AM

Either P or P[i] is a simple integer, not a list or other subscriptable type.
Was This Post Helpful? 0
  • +
  • -

#3 Ruru123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 09-October 11

Re: not subscriptable?

Posted 02 November 2011 - 10:54 AM

View PostJackOfAllTrades, on 02 November 2011 - 10:50 AM, said:

Either P or P[i] is a simple integer, not a list or other subscriptable type.


yes...but what does that even mean??

View PostRuru123, on 02 November 2011 - 10:53 AM, said:

View PostJackOfAllTrades, on 02 November 2011 - 10:50 AM, said:

Either P or P[i] is a simple integer, not a list or other subscriptable type.


yes...but what does that even mean??


I set P to equal 0 at the beginning of my code
Was This Post Helpful? 0
  • +
  • -

#4 poker158149  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 70
  • Joined: 29-June 11

Re: not subscriptable?

Posted 02 November 2011 - 11:20 AM

http://stackoverflow...riptable-or-not

Subscritable items are containers with other items in them, not single items like an integer, as you have it set.

This post has been edited by poker158149: 02 November 2011 - 11:21 AM

Was This Post Helpful? 2
  • +
  • -

#5 Ruru123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 09-October 11

Re: not subscriptable?

Posted 02 November 2011 - 11:51 AM

I am trying to write
set P to be an empty set

so in python I wrote it as: P=0

and then later on i wrote: P[i][j] = k
so P is a set..where i and j are variables

but I don't understand why I get 'int' object is not subscriptable
Was This Post Helpful? 0
  • +
  • -

#6 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: not subscriptable?

Posted 02 November 2011 - 02:06 PM

This is an empty set:

P = set()


Setting P to 0 doesn't result in an empty set. It may be worth noting that you can't index a set, such as P[i] as the elements aren't ordered.

You can only index things that can be indexed, such as lists or strings. Getting the different indexes of a single integer won't work - and doesn't even make sense.
Was This Post Helpful? 0
  • +
  • -

#7 Ruru123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 09-October 11

Re: not subscriptable?

Posted 02 November 2011 - 02:13 PM

View PostSimown, on 02 November 2011 - 02:06 PM, said:

This is an empty set:

P = set()


Setting P to 0 doesn't result in an empty set. It may be worth noting that you can't index a set, such as P[i] as the elements aren't ordered.

You can only index things that can be indexed, such as lists or strings. Getting the different indexes of a single integer won't work - and doesn't even make sense.


ok that got rid of the 'subscriptable' problem...but like you said, I got a typeError: 'set' object does not support indexing.

I am trying to write FloydPath's algorithm (iv got the pseudocode for it-but writing it in python is slightly different)

M = [[0, 3, 99, 4, 99],[99, 0, 4, 2, 1], [99, 99, 0, 99, 99], [4, 99, 99, 0, 3], [99, 99, 2, 2, 0]]

def FloydPath(L):
    P = set()
    for k in range(0, len(L)):
        for i in range(0, len(L)):
            for j in range(0, len(L)):
                if L[i][k] + L[k][j] < L[i][j]:
                    L[i][j] = L[i][k] + L[k][j]
                    P[i][k] = k
                    return L,P 
                
X = FloydPath(M)
print X




i,j,k are all just integers I guess
Was This Post Helpful? 0
  • +
  • -

#8 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: not subscriptable?

Posted 02 November 2011 - 02:21 PM

View PostSimown, on 02 November 2011 - 09:06 PM, said:

It may be worth noting that you can't index a set, such as P[i] as the elements aren't ordered.


Do you know what you would you like the output to be?
Was This Post Helpful? 0
  • +
  • -

#9 Ruru123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 09-October 11

Re: not subscriptable?

Posted 02 November 2011 - 02:26 PM

View PostSimown, on 02 November 2011 - 02:21 PM, said:

View PostSimown, on 02 November 2011 - 09:06 PM, said:

It may be worth noting that you can't index a set, such as P[i] as the elements aren't ordered.


Do you know what you would you like the output to be?


As I already stated earlier, I am trying to implement FloydPath's algorithm and therefore my desired outcome is that my final matrix M, will give me the cost of the shortest path between each pair of nodes.
Currently, M, only gives me the path between each pair of node.
(I think)

This post has been edited by Ruru123: 02 November 2011 - 02:27 PM

Was This Post Helpful? 0
  • +
  • -

#10 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: not subscriptable?

Posted 02 November 2011 - 02:31 PM

I'm not familiar with that particular algorithm. Could you provide the pseudocode you have?
Was This Post Helpful? 0
  • +
  • -

#11 Ruru123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 09-October 11

Re: not subscriptable?

Posted 02 November 2011 - 02:37 PM

View PostSimown, on 02 November 2011 - 02:31 PM, said:

I'm not familiar with that particular algorithm. Could you provide the pseudocode you have?


sure

FloydPath(L[1,...,n,1,...,n])
D=L
set P=0
for k=1 to n
do for i=1 to n
do for j=1 to n
do if D[i,k]+D[k,j]<D[i,j]
then D[i,j]=D[i,k]+D[k,j]
P[i,j]=k
return D and P
Was This Post Helpful? 0
  • +
  • -

#12 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: not subscriptable?

Posted 02 November 2011 - 02:48 PM

That's strange that it says set P = 0 and then indexes it.

It looks like you might need an array of 0s to mimic the other array

M = [[0, 3, 99, 4, 99],[99, 0, 4, 2, 1], [99, 99, 0, 99, 99], [4, 99, 99, 0, 3], [99, 99, 2, 2, 0]]
p = []

//set up array P, same array but a list of Os
for arr in M:
   P.append([0]*len(arr))


def FloydPath(L):
    for k in range(0, len(L)):
        for i in range(0, len(L)):
            for j in range(0, len(L)):
                if L[i][k] + L[k][j] < L[i][j]:
                    L[i][j] = L[i][k] + L[k][j]
                    P[i][k] = k
                    return L,P 
           
X = FloydPath(M)
print X




Then you can index P because it actually contains something. That should work. Is the output what you might expect?

This post has been edited by Simown: 02 November 2011 - 02:53 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1