Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

1-D array into a 2-D array results in wrong array values

My 1-D array is importing correctly and is displayed correctly, my logic also works when I do it by hand so I'm not sure what is wrong. When I copy each value in the 1-D array into the 2-D it is doing an odd pattern of copying as well as putting the wrong values in.

This is the 1-D array:

 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

This is the following output:

[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
row:  0  col:  0
0
In loop... [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
row:  0  col:  1
1
In loop... [[2, 0, 0, 0], [2, 0, 0, 0], [2, 0, 0, 0], [2, 0, 0, 0]]
row:  0  col:  2
2
In loop... [[3, 0, 0, 0], [3, 0, 0, 0], [3, 0, 0, 0], [3, 0, 0, 0]]
row:  0  col:  3
3
In loop... [[4, 0, 0, 0], [4, 0, 0, 0], [4, 0, 0, 0], [4, 0, 0, 0]]
row:  1  col:  0
4
In loop... [[4, 5, 0, 0], [4, 5, 0, 0], [4, 5, 0, 0], [4, 5, 0, 0]]
row:  1  col:  1
5
In loop... [[4, 6, 0, 0], [4, 6, 0, 0], [4, 6, 0, 0], [4, 6, 0, 0]]
row:  1  col:  2
6
In loop... [[4, 7, 0, 0], [4, 7, 0, 0], [4, 7, 0, 0], [4, 7, 0, 0]]
row:  1  col:  3
7
In loop... [[4, 8, 0, 0], [4, 8, 0, 0], [4, 8, 0, 0], [4, 8, 0, 0]]
row:  2  col:  0
8
In loop... [[4, 8, 9, 0], [4, 8, 9, 0], [4, 8, 9, 0], [4, 8, 9, 0]]
row:  2  col:  1
9
In loop... [[4, 8, 10, 0], [4, 8, 10, 0], [4, 8, 10, 0], [4, 8, 10, 0]]
row:  2  col:  2
10
In loop... [[4, 8, 11, 0], [4, 8, 11, 0], [4, 8, 11, 0], [4, 8, 11, 0]]
row:  2  col:  3
11
In loop... [[4, 8, 12, 0], [4, 8, 12, 0], [4, 8, 12, 0], [4, 8, 12, 0]]
row:  3  col:  0
12
In loop... [[4, 8, 12, 13], [4, 8, 12, 13], [4, 8, 12, 13], [4, 8, 12, 13]]
row:  3  col:  1
13
In loop... [[4, 8, 12, 14], [4, 8, 12, 14], [4, 8, 12, 14], [4, 8, 12, 14]]
row:  3  col:  2
14
In loop... [[4, 8, 12, 15], [4, 8, 12, 15], [4, 8, 12, 15], [4, 8, 12, 15]]
row:  3  col:  3
15
In loop... [[4, 8, 12, 16], [4, 8, 12, 16], [4, 8, 12, 16], [4, 8, 12, 16]]
Before return... [[4, 8, 12, 16], [4, 8, 12, 16], [4, 8, 12, 16], [4, 8, 12, 16]]

Here is the function code:

def makeTwoArr(array, height, width):
    print(array)
    newArray=[]
    line=[0]*width
    for i in range(height):
        newArray.append(line)
    location=0
    print(newArray)
    for row in range(height):
        for col in range(width):
            print("row: ",row," col: ",col);
            print(location)
##            #print(array[location])
            newArray[col][row]=array[location]
##            print(newArray)
            location+=1
            print("In loop...",newArray)

    print("Before return...",newArray)
    return newArray

Both me and my computer science professor can't figure out why the values are wrong, or why its filling certain spots at the wrong iteration.

like image 813
Brandon Williams Avatar asked Dec 28 '25 20:12

Brandon Williams


1 Answers

When you use line=[0]*width and then append(line) you actually create multiple references to the same line, hence when you modify one line in your loops you actually modify all lines. You need to actually create different lines such that values aren't modified everywhere by reference in the same list during assignment. It is always important to remember that lists are mutable, and appending the same one creates references to the same list.


As an aside, I'm guessing for your (educational) purposes you need to do assignment in this looping fashion, but if you were trying to do so more idiomatically with the standard library you could use iter and zip with unpacking to create lists of lists, although this would only work were you to be assured that your original list has the correct number of elements. This works by passing the same iterator to zip your desired number of times in order to unpack your entire sequence.

new_arr = list(map(list,zip(*[iter(arr)]*4)))

Outputs:

[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
like image 127
miradulo Avatar answered Dec 31 '25 11:12

miradulo