Skip to content Skip to sidebar Skip to footer

Python - Shift/delete Elements In A 2-dimensional Array

I need help with shifting and deleting elements in a 2-dimensional array. If the value in a list is negative and their is a list above it with positive values in the same locatio

Solution 1:

I work with columns separately, not with full rows.

  • search in column from bottom to top
  • find negative value
  • find positive value (bigger then zero) above
  • if not found then put zero in place of negative
  • if found then move down all value above move above to row, above-1 to row-1, above-2 to row-2, etc.

BTW: it is easier to search solution when rows are displayed one below another.

def move(data):

    # work in column, notwith full rows
    for col inrange(len(data)):

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

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

                # find positive value above
                above = row-1while above > -1and 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] = 0else:
                    # 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 -= 1return 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 scenario1():

    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 scenario2():

    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 scenario3(): #(This is the only one that I got working in my code below.)

    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)

# --- start scenarios ---

scenario1()
scenario2()
scenario3()

Results:

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]

debug: negative:-103debug:movedown4element(s)debug: negative:-103debug:movedown4element(s)debug: negative:-103debug:movedown4element(s)result:
[[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]]
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]]
expect==result:True---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]

debug: negative:-101debug:putzerodebug: negative:-101debug:putzerodebug: negative:-101debug:putzeroresult:
[[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]]
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]]
expect==result:True---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]

debug: negative:-102debug:movedown3element(s)debug: negative:-102debug:movedown3element(s)debug: negative:-102debug:movedown3element(s)result:
[[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]]
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]]
expect==result:True---

Solution 2:

Here's a simple numpy approach i.e

import numpy as np 
def get_arr(arr):
    arr = np.array(arr)
    arr[arr<1] = 0
    new_arr = arr[np.argsort(arr.sum(1)),:]
    return new_arr.tolist()

arr = [[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]]
arr1 = [[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]]
arr2 = [[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(get_arr(arr))
print(get_arr(arr1))
print(get_arr(arr2))
#[[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], [0, 2, 0, 0, 0], [0, 1, 2, 3, 2], [0, 3, 3, 2, 3]]
#[[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]]

Post a Comment for "Python - Shift/delete Elements In A 2-dimensional Array"