我尝试在Python中创建一个索引化的二维数组,但不管怎样,我总是遇到错误。
以下是代码:
#Declare Constants (no real constants in Python)
PLAYER = 0
ENEMY = 1
X = 0
Y = 1
AMMO = 2
CURRENT_STATE = 3
LAST_STATE = 4
#Initilise as list
information_state = [[]]
#Create 2D list structure
information_state.append([PLAYER,ENEMY])
information_state[PLAYER].append ([0,0,0,0,0])#X,Y,AMMO,CURRENT_STATE,LAST_STATE
information_state[ENEMY].append([0,0,0,0,0])#X,Y,AMMO,CURRENT_STATE,LAST_STATE
for index, item in enumerate(information_state):
print index, item
information_state[PLAYER][AMMO] = 5
创建此输出:
0 [[0, 0, 0, 0, 0]]
1 [0, 1, [0, 0, 0, 0, 0]]
IndexError: list assignment index out of range
我习惯使用PHP的数组,例如:
$array['player']['ammo'] = 5;
在Python中有类似的东西吗?我听说人们推荐使用numpy,但是我没有理解 :(
我刚接触Python。
注:使用的是Python 2.7版本。