4

I am having trouble with some code I am attempting to write.

I am attempting to take a list of lists of coordinates (representing possible positions of a shape in 3D) and form a list which consists of all the elements in the original list and additionally the elements in the original list rotated so that the [x, y, z] coordinates are shifted to include [z, x, y] and [y, z, x] also.

I think this is better illustrated with an example:

Taking the list (representing the possible positions of a 2x2x1 block, hence "two_by_two"):

two_by_two = [
    [[-1, -1, 1],  [-1, -1, 0],  [-1, 0, 0],   [-1, 0, 1]],
    [[-1, -1, 0],  [-1, -1, -1], [-1, 0, -1],  [-1, 0, 0]]
    ...
]

(the ellipses representing more similar lists of coordinates) I am attempting to form the complete list:

two_by_two_comp = [
    [[-1, -1, 1],  [-1, -1, 0],  [-1, 0, 0],   [-1, 0, 1]],
    [[-1, -1, 0],  [-1, -1, -1], [-1, 0, -1],  [-1, 0, 0]]
    ...
    [[1, -1, -1],  [0, -1, -1],  [0, -1, 0],   [1, -1, 0]],
    [[0, -1, -1],  [-1, -1, -1], [-1, -1, 0],  [0, -1, 0]]
    ...
    [[-1, 1, -1],  [-1, 0, -1],  [0, 0, -1],   [0, 1, -1]],
    [[-1, 0, -1],  [-1, -1, -1], [0, -1, -1],  [0, 0, -1]]
    ...
]

I hope that this is clear.

I am attempting to achieve this by using a function which shifts all of the coordinates in two_by_two:

# function to change [x, y, z] to [z, x, y]
def rotate_coordinates(parameter):
    coord_list = parameter[len(parameter) - 1]
    coordinates = coord_list[len(coord_list) - 1]

    z_coordinate = coordinates[2]
    coordinates.pop()
    coordinates.insert(0, z_coordinate)


# function to change list[x, y, z] to list[z, x, y]
def rotate_coord_list(parameter):
    coord_list = parameter[len(parameter) - 1]
    a = len(coord_list)
    while a > 0:
        coordinates = coord_list[len(coord_list) - 1]
        rotate_coordinates(parameter)
        coord_list.pop()
        coord_list.insert(0, coordinates)
        a = a - 1


# function to change list[list[x, y, z]] to list[list[z, x, y]]
def rotate_positions_list(parameter):
    b = len(parameter)
    while b > 0:
        coord_list = parameter[len(parameter) - 1]
        rotate_coord_list(parameter)
        parameter.pop()
        parameter.insert(0, coord_list)
        b = b - 1

This seems to me to be successful in that when I run:

print(two_by_two)
rotate_positions_list(two_by_two)
print(two_by_two)

It outputs:

[[[-1, -1, 1], [-1, -1, 0],  [-1, 0, 0],  [-1, 0, 1]], 
 [[-1, -1, 0], [-1, -1, -1], [-1, 0, -1], [-1, 0, 0]]
...]

[[[1, -1, -1], [0, -1, -1],  [0, -1, 0],  [1, -1, 0]], 
 [[0, -1, -1], [-1, -1, -1], [-1, -1, 0], [0, -1, 0]]
...]

And so it shifts all of the coordinates as I intended, the issue arises when I try to begin creating two_by_two_comp as so:

two_by_two_comp = []
two_by_two_comp.extend(two_by_two)
print(two_by_two_comp)

rotate_positions_list(two_by_two)
two_by_two_comp.extend(two_by_two)
print(two_by_two_comp)

Which returns:

[[[-1, -1, 1], [-1, -1, 0],  [-1, 0, 0],  [-1, 0, 1]], 
 [[-1, -1, 0], [-1, -1, -1], [-1, 0, -1], [-1, 0, 0]]
...]

[[[1, -1, -1], [0, -1, -1],  [0, -1, 0],  [1, -1, 0]], 
 [[0, -1, -1], [-1, -1, -1], [-1, -1, 0], [0, -1, 0]],
... 
 [[1, -1, -1], [0, -1, -1],  [0, -1, 0],  [1, -1, 0]], 
 [[0, -1, -1], [-1, -1, -1], [-1, -1, 0], [0, -1, 0]]
...]

So I end up with the same "version" of two_by_two copied as opposed to the shifted and original version, and I have no idea why the section of two_by_two_comp which I print out first gets affected by the rotate_positons_list(two_by_two) function.

If anyone could clear up my confusion, I would be very grateful. I will include the full script in one piece below.

Thank you, Dan

two_by_two = [
    [[-1, -1, 1],  [-1, -1, 0],  [-1, 0, 0],   [-1, 0, 1]],
    [[-1, -1, 0],  [-1, -1, -1], [-1, 0, -1],  [-1, 0, 0]],
    [[-1, 0, 0],   [-1, 0, -1],  [-1, 1, -1],  [-1, 1, 0]],
    [[-1, 0, 1],   [-1, 0, 0],   [-1, 1, 0],   [-1, 1, 1]],

    [[0, -1, 1],   [0, -1, 0],   [0, 0, 0],    [0, 0, 1]],
    [[0, -1, 0],   [0, -1, -1],  [0, 0, -1],   [0, 0, 0]],
    [[0, 0, 0],    [0, 0, -1],   [0, 1, -1],   [0, 1, 0]],
    [[0, 0, 1],    [0, 0, 0],    [0, 1, 0],    [0, 1, 1]],

    [[1, -1, 1],   [1, -1, 0],   [1, 0, 0],    [1, 0, 1]],
    [[1, -1, 0],   [1, -1, -1],  [1, 0, -1],   [1, 0, 0]],
    [[1, 0, 0],    [1, 0, -1],   [1, 1, -1],   [1, 1, 0]],
    [[1, 0, 1],    [1, 0, 0],    [1, 1, 0],    [1, 1, 1]],
]


# function to change [x, y, z] to [z, x, y]
def rotate_coordinates(parameter):
    coord_list = parameter[len(parameter) - 1]
    coordinates = coord_list[len(coord_list) - 1]

    z_coordinate = coordinates[2]
    coordinates.pop()
    coordinates.insert(0, z_coordinate)


# function to change list[x, y, z] to list[z, x, y]
def rotate_coord_list(parameter):
    coord_list = parameter[len(parameter) - 1]
    a = len(coord_list)
    while a > 0:
        coordinates = coord_list[len(coord_list) - 1]
        rotate_coordinates(parameter)
        coord_list.pop()
        coord_list.insert(0, coordinates)
        a = a - 1


# function to change list[list[x, y, z]] to list[list[z, x, y]]
def rotate_positions_list(parameter):
    b = len(parameter)
    while b > 0:
        coord_list = parameter[len(parameter) - 1]
        rotate_coord_list(parameter)
        parameter.pop()
        parameter.insert(0, coord_list)
        b = b - 1


two_by_two_comp = []
two_by_two_comp.extend(two_by_two)
print(two_by_two_comp)

rotate_positions_list(two_by_two)
two_by_two_comp.extend(two_by_two)
print(two_by_two_comp)
RMPR
  • 3,368
  • 4
  • 19
  • 31
Dan Kelly
  • 43
  • 3
  • 2
    [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – RMPR Oct 17 '20 at 18:54

1 Answers1

2

Your problem lies in the difference between deep copy and shallow copy. As per the docs

Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other.

The problematic line is thus:

two_by_two_comp.extend(two_by_two)

Let me illustrate with an example using two lists a and b:

a = [[2, 3, 4], [1, 2, 3]]
b = []
b.extend(a)

Now let's say I modify something inside a:

a[0].append(3)
print(a)   #  [[2, 3, 4, 3], [1, 2, 3]]

Everything is fine, but have a look at what happened to b in the meantime:

print(b)  #  [[2, 3, 4, 3], [1, 2, 3]]

It was also modified.

To achieve what you want, you need to create a deep copy of two_by_two otherwise you will just be referencing the same memory address. Long story short, instead of:

two_by_two_comp.extend(two_by_two)

You must do:

two_by_two_comp.extend(copy.deepcopy(two_by_two))

Don't forget to import the copy module at the top of your script:

import copy 
RMPR
  • 3,368
  • 4
  • 19
  • 31