为什么将此函数应用于未作为参数调用的变量?

Why is this function being applied to a variable which is not called as a parameter?

我在尝试编写一些代码时遇到问题。

我正在尝试获取一个坐标列表列表(表示 3D 形状的可能位置)并形成一个列表,该列表由原始列表中的所有元素以及原始列表中旋转的元素组成[x, y, z] 坐标移动到包括 [z, x, y] 和 [y, z, x]。

我认为用一个例子可以更好地说明这一点:

获取列表(表示 2x2x1 块的可能位置,因此为“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]]
    ...
]

(椭圆代表更相似的坐标列表)我试图形成完整的列表:

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]]
    ...
]

我希望这是清楚的。

我试图通过使用一个函数来实现这一点,该函数移动 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

这在我看来是成功的,因为我 运行:

print(two_by_two)
rotate_positions_list(two_by_two)
print(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, -1, -1], [0, -1, -1],  [0, -1, 0],  [1, -1, 0]], 
 [[0, -1, -1], [-1, -1, -1], [-1, -1, 0], [0, -1, 0]]
...]

所以它按照我的预期移动了所有坐标,当我尝试开始创建 two_by_two_comp 时出现了问题:

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)

哪个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]]
...]

所以我最终得到了复制的 two_by_two 的相同“版本”,而不是移位后的原始版本,我不知道为什么要打印出 two_by_two_comp 的部分首先受到 rotate_positons_list(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]],
    [[-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)

你的问题在于深拷贝和浅拷贝的区别。根据 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.

有问题的行是:

two_by_two_comp.extend(two_by_two)

让我用两个列表 ab:

来举例说明
a = [[2, 3, 4], [1, 2, 3]]
b = []
b.extend(a)

现在假设我修改了里面的东西 a:

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

一切都很好,但看看同时 b 发生了什么:

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

也修改了

要实现您想要的效果,您需要创建 two_by_two 的深层副本,否则您将只是引用相同的内存地址。长话短说,而不是:

two_by_two_comp.extend(two_by_two)

你必须做到:

two_by_two_comp.extend(copy.deepcopy(two_by_two))

不要忘记在脚本顶部导入复制模块:

import copy