根据坐标更改二维列表
Change a 2D list based of coordinates
我有一个二维列表,其中包含占位符变量以形成坐标系。
我还有一个具有不同坐标(y 和 x 索引)的列表,我想更改 2D 列表中的相应坐标,我想对所有坐标都这样做。
基本代码如下:
coordinate_system =
[['-'], ['-'], ['-'], ['-'], ['-']]
[['-'], ['-'], ['-'], ['-'], ['-']]
[['-'], ['-'], ['-'], ['-'], ['-']]
[['-'], ['-'], ['-'], ['-'], ['-']]
[['-'], ['-'], ['-'], ['-'], ['-']]
[['-'], ['-'], ['-'], ['-'], ['-']]
coordinates = [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [3, 5], [2, 4], [1, 3], [0, 2]]
我想以某种方式循环遍历坐标,以便我可以将相应的坐标替换为类似“x”的坐标。
--编辑--
我想要得到的输出是:
[['x'], ['-'], ['-'], ['-'], ['-']]
[['-'], ['x'], ['-'], ['-'], ['-']]
[['-'], ['-'], ['x'], ['-'], ['-']]
[['-'], ['x'], ['-'], ['x'], ['-']]
[['-'], ['-'], ['x'], ['-'], ['x']]
[['-'], ['-'], ['-'], ['x'], ['-']]
到目前为止我尝试过的代码是:
for i in range(len(coordinates)):
x = coordinates[i][0]
y = coordinates[i][1]
coordinate_system[y][x] = ["x"]
但列表中的所有项目都用此代码更改为“x”
(像这样)
[['x'], ['x'], ['x'], ['x'], ['x']]
[['x'], ['x'], ['x'], ['x'], ['x']]
[['x'], ['x'], ['x'], ['x'], ['x']]
[['x'], ['x'], ['x'], ['x'], ['x']]
[['x'], ['x'], ['x'], ['x'], ['x']]
[['x'], ['x'], ['x'], ['x'], ['x']]
在指定列表文字的方式以及对其进行索引的方式方面存在一些语法错误 - 这为您提供了所需的输出:
coordinate_system = [
[['-'], ['-'], ['-'], ['-'], ['-']],
[['-'], ['-'], ['-'], ['-'], ['-']],
[['-'], ['-'], ['-'], ['-'], ['-']],
[['-'], ['-'], ['-'], ['-'], ['-']],
[['-'], ['-'], ['-'], ['-'], ['-']],
[['-'], ['-'], ['-'], ['-'], ['-']]]
coordinates = [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [1, 3], [2, 4], [3,5]]
for coordinate in coordinates:
coordinate_system[coordinate[1]][coordinate[0]] = ['x']
coordinate_system
输出:
[[['x'], ['-'], ['-'], ['-'], ['-']],
[['-'], ['x'], ['-'], ['-'], ['-']],
[['-'], ['-'], ['x'], ['-'], ['-']],
[['-'], ['x'], ['-'], ['x'], ['-']],
[['-'], ['-'], ['x'], ['-'], ['x']],
[['-'], ['-'], ['-'], ['x'], ['-']]]
我有一个二维列表,其中包含占位符变量以形成坐标系。 我还有一个具有不同坐标(y 和 x 索引)的列表,我想更改 2D 列表中的相应坐标,我想对所有坐标都这样做。
基本代码如下:
coordinate_system =
[['-'], ['-'], ['-'], ['-'], ['-']]
[['-'], ['-'], ['-'], ['-'], ['-']]
[['-'], ['-'], ['-'], ['-'], ['-']]
[['-'], ['-'], ['-'], ['-'], ['-']]
[['-'], ['-'], ['-'], ['-'], ['-']]
[['-'], ['-'], ['-'], ['-'], ['-']]
coordinates = [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [3, 5], [2, 4], [1, 3], [0, 2]]
我想以某种方式循环遍历坐标,以便我可以将相应的坐标替换为类似“x”的坐标。
--编辑--
我想要得到的输出是:
[['x'], ['-'], ['-'], ['-'], ['-']]
[['-'], ['x'], ['-'], ['-'], ['-']]
[['-'], ['-'], ['x'], ['-'], ['-']]
[['-'], ['x'], ['-'], ['x'], ['-']]
[['-'], ['-'], ['x'], ['-'], ['x']]
[['-'], ['-'], ['-'], ['x'], ['-']]
到目前为止我尝试过的代码是:
for i in range(len(coordinates)):
x = coordinates[i][0]
y = coordinates[i][1]
coordinate_system[y][x] = ["x"]
但列表中的所有项目都用此代码更改为“x” (像这样)
[['x'], ['x'], ['x'], ['x'], ['x']]
[['x'], ['x'], ['x'], ['x'], ['x']]
[['x'], ['x'], ['x'], ['x'], ['x']]
[['x'], ['x'], ['x'], ['x'], ['x']]
[['x'], ['x'], ['x'], ['x'], ['x']]
[['x'], ['x'], ['x'], ['x'], ['x']]
在指定列表文字的方式以及对其进行索引的方式方面存在一些语法错误 - 这为您提供了所需的输出:
coordinate_system = [
[['-'], ['-'], ['-'], ['-'], ['-']],
[['-'], ['-'], ['-'], ['-'], ['-']],
[['-'], ['-'], ['-'], ['-'], ['-']],
[['-'], ['-'], ['-'], ['-'], ['-']],
[['-'], ['-'], ['-'], ['-'], ['-']],
[['-'], ['-'], ['-'], ['-'], ['-']]]
coordinates = [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [1, 3], [2, 4], [3,5]]
for coordinate in coordinates:
coordinate_system[coordinate[1]][coordinate[0]] = ['x']
coordinate_system
输出:
[[['x'], ['-'], ['-'], ['-'], ['-']],
[['-'], ['x'], ['-'], ['-'], ['-']],
[['-'], ['-'], ['x'], ['-'], ['-']],
[['-'], ['x'], ['-'], ['x'], ['-']],
[['-'], ['-'], ['x'], ['-'], ['x']],
[['-'], ['-'], ['-'], ['x'], ['-']]]