Python 更改基于坐标的二维列表
我有一个带有占位符变量的二维列表,以形成一个坐标系。 我还有一个具有不同坐标(y和x索引)的列表,我想更改2D列表中的相应坐标,我想对所有坐标执行此操作 以下是基本代码:Python 更改基于坐标的二维列表,python,list,loops,coordinates,coordinate-systems,Python,List,Loops,Coordinates,Coordinate Systems,我有一个带有占位符变量的二维列表,以形成一个坐标系。 我还有一个具有不同坐标(y和x索引)的列表,我想更改2D列表中的相应坐标,我想对所有坐标执行此操作 以下是基本代码: coordinate_system = [['-'], ['-'], ['-'], ['-'], ['-']] [['-'], ['-'], ['-'], ['-'], ['-']] [['-'], ['-'], ['-'], ['-'], ['-']] [['-'], ['-'], ['-'], ['-'], ['-']]
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”
(像这样)
在指定列表文字的方式以及索引到列表文字的方式中存在一些语法错误-这为您提供了所需的输出:
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'], ['-']]]
通过在输入中显示预期的输出,您可以使问题更加清楚?你尝试了什么?@DanielHao done:)是的,谢谢你这很好用:Dglad它有帮助:)
[[['x'], ['-'], ['-'], ['-'], ['-']],
[['-'], ['x'], ['-'], ['-'], ['-']],
[['-'], ['-'], ['x'], ['-'], ['-']],
[['-'], ['x'], ['-'], ['x'], ['-']],
[['-'], ['-'], ['x'], ['-'], ['x']],
[['-'], ['-'], ['-'], ['x'], ['-']]]