带有变量引用的Python列表

时间:2018-02-28 21:16:28

标签: python python-3.x list

我已将我的列表编号为layer1,layer2 ... 我想在调用它时使用变量(playery)作为列表名称中的数字,因此如果playery等于4,则列出的列表将是layer4

layer1 = [".", ".", ".", ".", "."]
layer2 = [".", ".", ".", ".", "."]
layer3 = [".", ".", "■", ".", "."]
layer4 = [".", ".", ".", ".", "."]
layer5 = [".", ".", ".", ".", "."]
playerx = (3)
playery = (4)
(layer(playery - 1))[playerx-1]= (".")

(layer(playery))[playerx-1]= ("■")

这是我到目前为止所做的,但它不起作用

2 个答案:

答案 0 :(得分:0)

这是你应该做的。

layers = [[".", ".", ".", ".", "."],
          [".", ".", ".", ".", "."],
          [".", ".", "■", ".", "."],
          [".", ".", ".", ".", "."],
          [".", ".", ".", ".", "."]]

>> playerx = 3
>> playery = 4
>> layers[playery - 1][playerx - 1]= "."
>> layers[playery][playerx - 1]= "."

答案 1 :(得分:0)

像这样创建2D数组:

layers= [layer1, layer2, layer3, layer4, layer5]

如果你想获得“■”,你应该输入:

layers[2][2]