Python - 在二维数组中移位/删除值

时间:2017-12-03 23:55:22

标签: python arrays

我需要帮助移动和删除二维数组中的元素。

  1. 如果列表中的值为负数,并且它们是位于其上方的列表,并且在同一位置具有正值。它应该将所有内容都降低,导致负值消失。

  2. 如果上面没有任何列表,或者上面列表中的相应值只是0.它会将负值替换为0。

  3. 场景1,3和4正在运行!但是场景2不起作用。 (我希望我在我的例子中涵盖了所有可能的场景)

    注意:正值不应该消失,它们只能在需要时向下移动。只有负值(低于-100)消失。

    这些例子应该更好地解释它:

    场景1: #The Works

    DATA

    [[   0,    0,    0, 0, 0],
     [   0,    0,    0, 0, 0],
     [   1,    3,    1, 0, 0],
     [-102, -102, -102, 0, 0],
     [   3,    1,    3, 0, 0]]
    

    EXPECT

    [[0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0],
     [1, 3, 1, 0, 0],
     [3, 1, 3, 0, 0]]
    

    场景2:#不起作用

    DATA

    [[   0,    0,    0, 0, 0],
     [   0,    0,    0, 0, 0],
     [   1,    2,    1, 0, 0],
     [   2,    1,    2, 0, 0],
     [-103, -103, -103, 0, 0]]
    

    EXPECT

    [[0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0],
     [1, 2, 1, 0, 0],
     [2, 1, 2, 0, 0]]
    

    当前输出(不正确):

    [[0, 0, 0, 0, 0], 
     [0, 0, 0, 0, 0], 
     [1, 2, 1, 0, 0], 
     [2, 1, 2, 0, 0], 
     [0, 0, 0, 0, 0]] 
    

    场景3: #The Works

    DATA

    [[0, 0,    0,    0,    0],
     [0, 0,    0,    0,    0],
     [0, 2, -101, -101, -101],
     [0, 1,    2,    3,    2],
     [0, 3,    3,    2,    3]]
    

    EXPECT

    [[0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0],
     [0, 2, 0, 0, 0],
     [0, 1, 2, 3, 2],
     [0, 3, 3, 2, 3]]
    

    场景4: #The Works

    DATA

    [[ 0, 0, 0, 0, 0],
     [ 0, 0, 0, 0, 0],
     [-101, 2, 2, 3, 4],
     [-101, 1, 2, 3, 2],
     [-101, 3, 3, 2, 3]]
    

    EXPECT

    [[0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0],
     [0, 2, 2, 3, 4],
     [0, 1, 2, 3, 2],
     [0, 3, 3, 2, 3]]
    

    这是我的代码 - 对于我想要实现的目标而言,它肯定过于复杂但我一直坚持这一点,无法找出任何替代方案。 (我不能导入任何外部函数......所以没有numpy)

    def move(data):
    
        # PART 1
        '''
        Creates a list that contains a tuple (row, coll) of the location where a negative value (> -100) appears on the list.  
        '''
    
        rows = len(data)
    
        c_count = 4
    
        row_list = []
    
        while c_count >= 0:
            for x in range(rows):
                if data[x][c_count] < -100:
                    row_list.append((x, c_count))
    
            c_count -=1
    
    
    
        # PART 2
        '''
        Iterates through the list of values that contain negative value (> -100) and performs the actions listed below. 
        '''
    
        for x in row_list:
            row = x[0]
            col = x[1]
    
            try: # If there isn't anything above the negative value (except for 0), make all negative value (>-100) == 0. EXAMPLE (DATA 3) 
    
                if data[row-1][col] == 0:
                     data[row][col] = 0
    
            except(IndexError):
                pass
    
            try: # If a row of negative values is between a row on top and bottom that contains possitive values, then merge the values above with the negative values below it. EXAMPLE (DATA 1)
    
                if data[row-1][col] > 0 and data[row+1][col] > 0:
    
                    c_count = 4
                    while c_count >= 0:
    
                        count = len(data) - 1
                        prev = count - 1
    
                        while count > 0 and prev >= 0:
                            if data[count][c_count] < -100:
    
                                while prev >= 0 and data[prev][c_count] == 0:
                                    prev -= 1
    
                                data[count][c_count] = data[prev][c_count]
                                data[prev][c_count]= 0
    
                            count -= 1
                            prev -= 1
    
                        c_count -= 1
                    return data
    
            except(IndexError):
                pass
    
            try: # If a row of negative values has nothing underneath it (at the bottom) of the list. Then push everything on the top down replacing the negative value. This isn't working! 
    
    **SCENARIO 2 Should Have Worked Here**
    
                if data[row-1][col] > 0:
                    data[row][col] = 0
    
                    rows = len(data)
    
                    c_count = 4
                    while c_count >= 0:
    
                        for x in range(rows):
                            if data[x][c_count] > 0:
                                rows = x
                                break
    
                        last = rows-1
    
                        if data[last][c_count] == 0:
                            while last > 0:
                                data[last][c_count] = data[last-1][c_count]
                                last -= 1   
                            data[0][c_count] = 0
    
                        c_count -= 1
    
    
    
            except(IndexError):
                pass
    
    
    
        return data
    
    print('Data 1') # This Works
    data1 = [[0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0],
             [1, 3, 1, 0, 0],
             [-102, -102, -102, 0, 0],
             [3, 1, 3, 0, 0]]
    
    print(data1, 'org')
    x = move(data1)
    
    expect1 = [[0, 0, 0, 0, 0],
               [0, 0, 0, 0, 0],
               [0, 0, 0, 0, 0],
               [1, 3, 1, 0, 0],
               [3, 1, 3, 0, 0]]
    
    print(x, 'sol')
    print(expect1, 'expect')
    print(data1 == expect1)
    
    print()
    
    print('Data 2') # Doesn't Work
    
    data2 = [[0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0],
             [1, 2, 1, 0, 0],
             [2, 1, 2, 0, 0],
             [-103, -103, -103, 0, 0]]
    
    print(data2, 'org')
    y = move(data2)
    
    expect2 = [[0, 0, 0, 0, 0],
               [0, 0, 0, 0, 0],
               [0, 0, 0, 0, 0],
               [1, 2, 1, 0, 0],
               [2, 1, 2, 0, 0]]
    
    print(y,'sol')
    print(expect2, 'expect')
    print(data2 == expect2)
    
    print()
    
    print('Data 3') # This Works
    
    data3 = [[0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0],
             [0, 2, -101, -101, -101],
             [0, 1, 2, 3, 2],
             [0, 3, 3, 2, 3]]
    print(data3, 'org')
    z = move(data3)
    
    expect3 = [[0, 0, 0, 0, 0],
               [0, 0, 0, 0, 0],
               [0, 2, 0, 0, 0],
               [0, 1, 2, 3, 2],
               [0, 3, 3, 2, 3]]
    
    print(z,'sol')
    print(expect3, 'expect')
    print(data3 == expect3)
    
    print()
    
    print('Data 4') # This Works
    
    data4 = [[0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0],
             [-101, 2, 2, 3, 4],
             [-101, 1, 2, 3, 2],
             [-101, 3, 3, 2, 3]]
    
    print(data4, 'org')
    a = move(data4)
    
    expect4 = [[0, 0, 0, 0, 0],
               [0, 0, 0, 0, 0],
               [0, 2, 2, 3, 4],
               [0, 1, 2, 3, 2],
               [0, 3, 3, 2, 3]]
    
    print(a,'sol')
    print(expect4, 'expect')
    print(data4 == expect4)
    

1 个答案:

答案 0 :(得分:1)

我使用上一个问题的代码 Python - Shift/Delete Elements in a 2-Dimensional Array
它给出了正确的结果:

我单独使用列,而不是使用完整行。

  • 从下到上搜索列
  • 找到负值
  • 上方找到正值(大于零)
  • 如果没有找到,则将零代替否定
  • 如果找到则向下移动所有值 (将var knexObj = { getAllRows: function() { return knex('table').select("*"); } } knexObj.getAllRows().then(function (data) { console.log(data); }); 移至aboverow移至above-1row-1移至above-2等。

row-2

结果:

def move(data):

    # work in column, not with full rows
    for col in range(len(data)):

        # move from bottom to top
        for row in range(len(data[0])-1, -1, -1):

            # check if negative value
            if data[row][col] < 0:
                print('debug: negative:', data[row][col])

                # find positive value above
                above = row-1
                while above > -1 and data[above][col] <= 0:
                    above -= 1

                # check if found positive value 
                if above == -1:
                    # put zero if not found value above
                    print('debug: put zero')
                    data[row][col] = 0
                else:
                    # move down all values above 
                    print('debug: move down', above+1, 'element(s)')
                    while above > -1:
                        data[row][col] = data[above][col]
                        data[above][col] = 0
                        row -= 1
                        above -= 1

    return data

# --- function to run one scenario, display data and check result ---

def run(data, expect):

        print('data:')
        print('\n'.join(str(row) for row in data))
        print()
        result = move(data)
        print()
        print('result:')
        print(result)
        print('expect:')
        print(expect)
        print('expect == result:', expect == result)
        print('---')

# --- scenarios ---

def scenario_B1():

    DATA = [
        [   0,    0,    0, 0, 0],
        [   0,    0,    0, 0, 0],
        [   1,    3,    1, 0, 0],
        [-102, -102, -102, 0, 0],
        [   3,    1,    3, 0, 0]
    ]

    EXPECT =    [
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [1, 3, 1, 0, 0],
        [3, 1, 3, 0, 0]
    ]

    run(DATA, EXPECT)

def scenario_B2():

    DATA = [
        [   0,    0,    0, 0, 0],
        [   0,    0,    0, 0, 0],
        [   1,    2,    1, 0, 0],
        [   2,    1,    2, 0, 0],
        [-103, -103, -103, 0, 0]
    ]

    EXPECT = [
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [1, 2, 1, 0, 0],
        [2, 1, 2, 0, 0]
    ]

    run(DATA, EXPECT)

def scenario_B3():

    DATA = [
        [0, 0,    0,    0,    0],
        [0, 0,    0,    0,    0],
        [0, 2, -101, -101, -101],
        [0, 1,    2,    3,    2],
        [0, 3,    3,    2,    3]
    ]

    EXPECT = [
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 2, 0, 0, 0],
        [0, 1, 2, 3, 2],
        [0, 3, 3, 2, 3]
    ]

    run(DATA, EXPECT)

def scenario_B4():

    DATA = [
        [ 0, 0, 0, 0, 0],
        [ 0, 0, 0, 0, 0],
        [-101, 2, 2, 3, 4],
        [-101, 1, 2, 3, 2],
        [-101, 3, 3, 2, 3]
    ]

    EXPECT = [
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 2, 2, 3, 4],
        [0, 1, 2, 3, 2],
        [0, 3, 3, 2, 3]
    ]

    run(DATA, EXPECT)

# --- start scenarios ---

scenario_B1()
scenario_B2()
scenario_B3()
scenario_B4()