Thursday 15 April 2010

python - 'int' object is not subscriptable -


My code for printing a matrix as a class is as follows:

  i Range (1, lane (a [ii])): range (1, lane (a) for line = "\ t" "+ str (a [i] [0]) [I]): line = line + "\ t" + str (a [i] [j]) line = line + "|   

Where there is a list [22, 38, 30, 58, 34, 69]

It should look

| 22 38 |

| 30 58 |

| 34 | 69 |

It is coming back 'int' is not responsible for the line in the object code :

  line = "\ t" "+ str (a [i] [0])   

How can I fix it?

A is a list - a one-dimensional array A [0] is a int , which is not subscriptable because of your error.

It is more natural to represent the matrix in the form of a two-dimensional list in Python, such as:

  A = [[22, 38], [30, 58], [34, 69]]   

or

  A = [[22, 30, 34], [38, 58, 69]]   

If you do this then your code will work, however it would be better to use the code like this:

  def print_matrix (M): For M in M: print ("| {} |" .format ('\' for O for O (O))) print_matrix (a)   

or Even the bus:

  If you are tied to your single dimension for A, you can print it like this:  
  Def print_list_metrics (m, width): for i, zip (range (0, lane (m), width), range (skip, lane (m) + width, width)): print ('| {} |'   

Or like it:

for O in (.frat ("str (hey)) for .format (" \ t " P>

  def print_list_matrix (m, width): for range i (0, lane (m), width): print ('| {} | ' .format ("\ t" to join M (in SR (M) [i: i + i width]])   

or I Want to avoid:

No comments:

Post a Comment