0

i'm solving a leetcode question using 2 d array in python. When i try to put the element in a specific location in 2d array , it is inserting in all the corresponding column.

For example if i try to insert an element in 1st row 1st column, it is inserting the element in rows in first column.

In the code line : a[i][j] = s[si]

Can anyone help me how to get rid off and help me tell why this is happening ?

This is the code:

class Solution:
    def convert(self, s: str, numRows: int) -> str:
        
        
        n = len(s)
        
        
        
        m = math.ceil(n/(2*numRows-1))
        
        m = int(m)
        
        col = m*n
        
        a = [[0]*col]*numRows
        
        
        i = 0
        
        j = 0
        
        si = 0
        
        up = False
        down = True
        
        while(si<n):
        
            
            
            for _ in range(numRows):
                
                if(down  and si<n):
            
                    a[i][j] = s[si]
                
                    #print(a[i][j])
                    print("down si",s[si],i,j)
                    print(a)
                    
                    i = i+1
                    
                    si = si+1
            
                    if((i) == numRows ):
                    
                        #print("array before up",a)
                        i = numRows-2
                        j = j+1
                        up = True
                        down = False
                        
       
            for _ in range(numRows):
                
                if(up):
                    
                    if(i==0):
                        
                        i = 0
                        
                        
                        
                        up = False
                        
                        down = True
                        
                    else:
                        
                        
                        a[i][j] = s[si]
                        
                        #print("up si",s[si],i,j)
                        #print(a)
                        
                        si = si+1
                        
           
                        i = i-1
                        j = j+1       
                        
                        
        print(a)      
  • Does this answer your question? [List of lists changes reflected across sublists unexpectedly](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) – Mechanic Pig Oct 06 '22 at 02:41

1 Answers1

0

It's because you are using the * operator to create 2d-arrays. If you use * on mutables, Python creates a shallow copy of the object. Thus, when you create your 2D-array, you are really instantiating an array of references to the samme array in each column. This is why I always use list comprehensions when creating similar 2D-arrays. For example creating a 2D-array of size N*M filled with 0:

arr = [[0 for _ in range(M)] for _ in range(N)]

In your case, I would change

a = [[0]*col]*numRows

To

a = [[0 for _ in range(col)] for _ in range(numRows)]
chrille0313
  • 111
  • 4