Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

An other error on Python

  1. Dec 22, 2011 #1
    Hi all!

    I'm learning programming on Python.
    Currently, I'm trying to implement a recursive algorithm using a divide and conquer method to compute a product of matrices.

    Yet an error occurs when I try to assign an element in a list (representing a matrix)

    I can't understand where the problem comes from, since I tried on the shell to enter a matrix exactly as in the program and assign its values (C[0][0]=...) and it works.
    But when I try to execute the program, the following error is raised:


    line 42, in sqmat_mult_rec
    C[0][0]=A[0][0]*B[0][0]
    TypeError: 'int' object is not subscriptable



    Any idea?

    (code attached)
     

    Attached Files:

    Last edited: Dec 22, 2011
  2. jcsd
  3. Dec 22, 2011 #2

    phyzguy

    User Avatar
    Science Advisor

    Why don't you try printing out A, B and C right before the error occurs? The 'not subscriptable' error usually means that you are trying to look at a subscript that doesn't exist.
     
  4. Dec 22, 2011 #3
    I tried to and actually each of the objects I try to assign exist...
     
  5. Dec 22, 2011 #4

    phyzguy

    User Avatar
    Science Advisor

    I'm sure they exist, but are they nested lists? When you write A[0][0], it means A must be of the form [[something]]. If A is a simple variable (which is what the message implies) or a simple list [], then you will get an error when you try to access A[0][0]. What does the print out of A and B look like?
     
  6. Dec 22, 2011 #5

    jhae2.718

    User Avatar
    Gold Member

    Try adding
    Code (Text):

    print(type(A))
    print(type(B))
     
    before that call to see what type is getting passed. From the error, A or B are integers.
     
  7. Dec 25, 2011 #6
    A and B are nested lists indeed.
    They look like A=[[1,2,3],[2,3,4],[2,2,3]] for instance.
    But what amazes me is that when I try to work with A[0][0] in the python shell, then there is absolutely no problem whereas with my program (with IDLE) an error is raised...
     
  8. Dec 25, 2011 #7
    I get type list
     
  9. Dec 26, 2011 #8

    jhae2.718

    User Avatar
    Gold Member

    Edit: Your problem is that when [itex]n[/itex] is 1, A and B are no longer nested lists.

    Then, the subscripting fails because A[0][0] doesn't exist when e.g. A = [1]
     
    Last edited: Dec 26, 2011
  10. Jan 2, 2012 #9
    sorry for the delay I had no internet access for a while
    thank you for your help jhae2.718, it looks like this was the problem indeed!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: An other error on Python
  1. Error in python book? (Replies: 3)

  2. Error on Python (Replies: 9)

  3. Key error on python (Replies: 0)

Loading...