当需要对一个空列表进行赋值时,可以用APPEND或者其他函数,
但不可以对空列表使用索引赋值
MM = []
MM[0] = 9
print(MM) # 报错
AA = []
AA.append(9)
print(AA) #正确
正确的案例
def revert(block_one):
# 俄罗斯方块中的某个应用函数
# 功能是将 [[5, 2], ['blue'], [[0, 1], [0, 0], [0, -1], [0, -2]]]转换成:[[5, 3], [5, 2], [5, 1], [5, 0]]
shape_xy_d = block_one[0]
shape_one_d = block_one[2]
new_shapes = []
for x in shape_one_d:
new_shape = [0, 0]
# new_shape = []
new_shape[0] = shape_xy_d[0] + x[0]
new_shape[1] = shape_xy_d[1] + x[1]
new_shapes.append(new_shape)
return new_shapes
one = [[5, 2], ['blue'], [[0, 1], [0, 0], [0, -1], [0, -2]]]
new_one = revert(one)
print(new_one)
错误的写法:
def revert(block_one):
# 俄罗斯方块中的某个应用函数
# 功能是将 [[5, 2], ['blue'], [[0, 1], [0, 0], [0, -1], [0, -2]]]转换成:[[5, 3], [5, 2], [5, 1], [5, 0]]
shape_xy_d = block_one[0]
shape_one_d = block_one[2]
new_shapes = []
for x in shape_one_d:
new_shape = [] # 此处错误
new_shape[0] = shape_xy_d[0] + x[0]
new_shape[1] = shape_xy_d[1] + x[1]
new_shapes.append(new_shape)
return new_shapes
one = [[5, 2], ['blue'], [[0, 1], [0, 0], [0, -1], [0, -2]]]
new_one = revert(one)
print(new_one)