List of lists: indexing and assigning values (Python) -
I'm sure this stupid is simple, but in some way I can not understand it.
  a = [[0] * 4] * 3 a [0] [0] = 1 a [[1, 0, 0, 0], [1, 0, 0, 0 ], [1, 0, 0, 0]]    I'm just trying to allocate 1 to the first element in the first row. Instead, the value of the first element 1 is assigned to the  every  line. How should I indexing so that value can be assigned to the right element?   Thank you!  
 
  First you should understand this fact.  
 If you do  
  A = [5] * 3    It is a surprise that  a [0] ,  a [1]  and  one [2]  are the same thing you can do to ensure this by doing so.    id (a [0]) == ID (a [1]) == ID (a [2]) #True    Sounds like the following.  
  a [0] - + a [1] - + - - 5a [2] - +    If you do  
  a [0] = 10    The following is the like.  
  a [0] ----- & gt; 10 A [1] - + - - 5a [2] - +    OK. Now back to the list of your list's list. To simplify your problem, define  a :    a = [[4, 5, 6]] * 3    Unknowingly these are similar objects.  
  id (a [0]) == ID (a [1]) == ID (a [2]) #true    Sounds like the following.  
  a [0] - + a [1] - + - - [4, 5, 6] A [2] - +    If you  
  a [0] = 10    So it is like the following.  
  a [0] ----- & gt; 10 A [1] - + - - [4, 5, 6] A [2] - +    OK This is not a problem. Then you do this  
  a [1] [0] = 8    So it is like the following.  
  a [0] ----- & gt; 10 A [1] - + - - [8, 5, 6] One [2] - +    Done !! It's easy to understand.  
 HIH. :)   
 
Comments
Post a Comment