功能:在列表中按不同方向移动项目?

2024-10-05 14:23:47 发布

您现在位置:Python中文网/ 问答频道 /正文

如果可以在列表中移动值,我需要返回一个布尔值是字符串可以移动到的空白点。对于第一个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']]

我计划编写第二个函数,它只在列表中上下移动值。我需要在上面的函数中做什么改变才能使它像变量一样在列表中上下移动?感谢您的帮助/建议。我试着保持它的简单,只使用循环和布尔函数,很少有像听起来那样乏味的内置函数。你知道吗


Tags: and函数inright列表forlenreturn
2条回答
def shift_horizontal(value, direction, area):#direction True is left
    result = []
    for lane in area:
        if not direction:
            lane = reversed(lane)
        try:
            ind = lane.index(value)
            result.append( '.' in lane[:index])
        except ValueError:
            result.append(False)
    return result

返回布尔值列表。根据方向翻转列表。然后找到value最左边的索引。然后检查列表中的'.'。不确定这是否正是你想要的。如果你想看看是否有一个相邻的'.',那么改为if lane[index-1] == '.'

您只需使用-1&;+1即可,而不用左右。
要在一次运行中移动所有“A”:
如果方向是+1(向右),则从右到左遍历内部列表,尝试移动遇到的每个“A”,直到该行的末尾。
如果方向是-1(向左),则从左向右迭代。你知道吗

area1 = [['.', '.', '.', 'e'], 
         ['A', 'A', '.', 'e'], 
         ['.', 'X', '.', 'e'], 
         ['.', 'X', 'Y', 'Y'], 
         ['.', '.', 'Z', 'Z']]

def move_horizontal(table, value, direction):
    is_anything_moved = False
    for row in table:
        width = len(row)
        # Traverse the row (skipping the first element) from right to left if direction is 1. 
        for x in (range(1,width,1) if direction==-1 else range(width-2,-1,-1)): 
            if row[x]==value: #found a value match, now move it as far as we can
                i = x+direction # new index of value
                while i<width and i>=0 and row[i]=='.':
                    row[i-direction] = '.'
                    row[i] = value
                    i += direction
                    is_anything_moved = True
    return is_anything_moved

[print(row) for row in area1]
print()

move_horizontal(area1,'A',1)

[print(row) for row in area1]
print()

move_horizontal(area1,'e',-1)

[print(row) for row in area1]

这应该输出:

['.', '.', '.', 'e']
['A', 'A', '.', 'e']
['.', 'X', '.', 'e']
['.', 'X', 'Y', 'Y']
['.', '.', 'Z', 'Z']

['.', '.', '.', 'e']
['.', 'A', 'A', 'e']
['.', 'X', '.', 'e']
['.', 'X', 'Y', 'Y']
['.', '.', 'Z', 'Z']

['e', '.', '.', '.']
['.', 'A', 'A', 'e']
['.', 'X', 'e', '.']
['.', 'X', 'Y', 'Y']
['.', '.', 'Z', 'Z']

相关问题 更多 >