如果可以在列表中移动值,我需要返回一个布尔值是字符串可以移动到的空白点。对于第一个func,如何将相同的值左右移动?我不知道如何定义左变量和右变量,我想知道是否需要pop。()删除我将变量移动到的位置。你知道吗
area1 = [['.', '.', '.', 'e'],
['A', 'A', '.', 'e'],
['.', 'X', '.', 'e'],
['.', 'X', 'Y', 'Y'],
['.', '.', 'Z', 'Z']]
def shift_horizontal(value, direction, area):
for i in range(len(area)-1):
for j in range(len(row)-1):
if right and value[i] == '.'[i+1]: # Will shift right if value next is '.'
return True
elif left and value[i] == '.'[i-1]: # Will shift left if value next is '.'
return True
else:
return False
shift_horizontal('A', right, area1)
之后列表应该是什么样子(我实际上不需要返回列表,只是一个视觉参考):
area1 = [['.', '.', '.', 'e'],
['.', 'A', 'A', 'e'],
['.', 'X', '.', 'e'],
['.', 'X', 'Y', 'Y'],
['.', '.', 'Z', 'Z']]
我计划编写第二个函数,它只在列表中上下移动值。我需要在上面的函数中做什么改变才能使它像变量一样在列表中上下移动?感谢您的帮助/建议。我试着保持它的简单,只使用循环和布尔函数,很少有像听起来那样乏味的内置函数。你知道吗
返回布尔值列表。根据方向翻转列表。然后找到
value
最左边的索引。然后检查列表中的'.'
。不确定这是否正是你想要的。如果你想看看是否有一个相邻的'.'
,那么改为if lane[index-1] == '.'
您只需使用-1&;+1即可,而不用左右。
要在一次运行中移动所有“A”:
如果方向是+1(向右),则从右到左遍历内部列表,尝试移动遇到的每个“A”,直到该行的末尾。
如果方向是-1(向左),则从左向右迭代。你知道吗
这应该输出:
相关问题 更多 >
编程相关推荐