grid = np.empty((10,0)).tolist()
coords = grid.copy()
distances = grid.copy()
print(id(grid), id(coords), id(distances))
for i in range(10):
for j in range(10):
distances[i].append(i+j)
coords[i].append([i, j])
print(distances)
print(coords)
I created a nested list for turning into a 2D grid. I want to create two different 2D grid, one containing the coordinate of each element, the other containing the distance of each coordinate from (0,0).
I used the .copy() method twice, and confirmed that coords
and distances
are in fact different locations in memory. So why does my code output both as the same incorrect list?
Output:
[[0, [0, 0], 1, [0, 1], 2, [0, 2], 3, [0, 3], 4, [0, 4], 5, [0, 5], 6, [0, 6], 7, [0, 7], 8, [0, 8], 9, [0, 9]], [1, [1, 0], 2, [1, 1], 3, [1, 2], 4, [1, 3], 5, [1, 4], 6, [1, 5], 7, [1, 6], 8, [1, 7], 9, [1, 8], 10, [1, 9]], [2, [2, 0], 3, [2, 1], 4, [2, 2], 5, [2, 3], 6, [2, 4], 7, [2, 5], 8, [2, 6], 9, [2, 7], 10, [2, 8], 11, [2, 9]], [3, [3, 0], 4, [3, 1], 5, [3, 2], 6, [3, 3], 7, [3, 4], 8, [3, 5], 9, [3, 6], 10, [3, 7], 11, [3, 8], 12, [3, 9]], [4, [4, 0], 5, [4, 1], 6, [4, 2], 7, [4, 3], 8, [4, 4], 9, [4, 5], 10, [4, 6], 11, [4, 7], 12, [4, 8], 13, [4, 9]], [5, [5, 0], 6, [5, 1], 7, [5, 2], 8, [5, 3], 9, [5, 4], 10, [5, 5], 11, [5, 6], 12, [5, 7], 13, [5, 8], 14, [5, 9]], [6, [6, 0], 7, [6, 1], 8, [6, 2], 9, [6, 3], 10, [6, 4], 11, [6, 5], 12, [6, 6], 13, [6, 7], 14, [6, 8], 15, [6, 9]], [7, [7, 0], 8, [7, 1], 9, [7, 2], 10, [7, 3], 11, [7, 4], 12, [7, 5], 13, [7, 6], 14, [7, 7], 15, [7, 8], 16, [7, 9]], [8, [8, 0], 9, [8, 1], 10, [8, 2], 11, [8, 3], 12, [8, 4], 13, [8, 5], 14, [8, 6], 15, [8, 7], 16, [8, 8], 17, [8, 9]], [9, [9, 0], 10, [9, 1], 11, [9, 2], 12, [9, 3], 13, [9, 4], 14, [9, 5], 15, [9, 6], 16, [9, 7], 17, [9, 8], 18, [9, 9]]]