Home > Mobile >  How to strip a 2d array in python?
How to strip a 2d array in python?

Time:03-23

For editors: this is NOT stripping all strings in an array but stripping the array itself

So suppose i have an array like this:

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

I want a function stripArray(0, array) where the first argument is the "empty" value. After applying this function i want the returned array to look like this:

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

Values that were marked as empty (in this case 0) were stripped from the right and bottom sides. How would I go about implementing such a function? In the real case where I want to use it in the array instead of numbers there are dictionaries.

CodePudding user response:

It is better to do this vectorized

import numpy as np
arr = np.array([[0, 1, 8, 4, 0, 0],
                [1, 2, 3, 0, 0, 0], 
                [3, 2, 3, 0, 5, 0],
                [0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0]])
def stripArray(e, arr):
    return arr[(arr!=e).any(axis = 1), :][:, (arr!=e).any(axis = 0)]
stripArray(0, arr)
array([[0, 1, 8, 4, 0],
       [1, 2, 3, 0, 0],
       [3, 2, 3, 0, 5]])

CodePudding user response:

Here is an answer which doesnt need numpy:

from typing import List, Any

def all_value(value: Any, arr: List[float]) -> bool:
    return all(map(lambda x: x==value, arr))

def transpose_array(arr: List[List[float]]) -> List[List[float]]:
    return list(map(list, zip(*arr)))


def strip_array(value: Any, arr: List[List[float]]) -> List[List[float]]:
    # delete empty rows
    arr = [row for row in arr if not all_value(value, row)]

    #transpose and delete empty columns
    arr = transpose_array(arr)
    arr = [col for col in arr if not all_value(value, col)]

    #transpose back
    arr = transpose_array(arr)
    return arr

test = [[0, 1, 8, 4, 0, 0],
 [1, 2, 3, 0, 0, 0],
 [3, 2, 3, 0, 5, 0],
 [0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0]]

result = strip_array(0, test)

Output:

result
[[0, 1, 8, 4, 0],
 [1, 2, 3, 0, 0],
 [3, 2, 3, 0, 5]]

CodePudding user response:

Code:

def strip_array(array, empty_val=0):
    num_bad_columns = 0
    while np.all(array[:, -(num_bad_columns 1)] == 0):
        num_bad_columns  = 1
    array = array[:, :(-num_bad_columns)]
    num_bad_rows = 0
    while np.all(array[-(num_bad_rows 1), :] == 0):
        num_bad_rows  = 1
    array = array[:(-num_bad_rows), :]
    return array


array = np.array(
    [[0, 1, 8, 4, 0, 0],
    [1, 2, 3, 0, 0, 0],
    [3, 2, 3, 0, 5, 0],
    [0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0]]
)

print(array)
print(strip_array(array, 0))

Output:

[[0 1 8 4 0 0]
 [1 2 3 0 0 0]
 [3 2 3 0 5 0]
 [0 0 0 0 0 0]
 [0 0 0 0 0 0]]
[[0 1 8 4 0]
 [1 2 3 0 0]
 [3 2 3 0 5]]

CodePudding user response:

try using np.delete to remove unwanted rows or columns

data=[[0, 1, 8, 4, 0, 0],
 [1, 2, 3, 0, 0, 0], 
 [3, 2, 3, 0, 5, 0],
 [0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0]]

def drop_row(data):
    lstIdx=[]
    for i in range(len(data)):
        count=0
        for j in range(len(data[i])):
            if data[i][j] == 0:
                count =1
        if count==len(data[i]):
            print("row zero")
            lstIdx.append(i)
    
    #for i in lstIdx:
    data=np.delete(data,lstIdx,axis=0)
    return data

def drop_column(data):
    lstIdx=[]
    if len(data)==0:
        return data
    for j in range(len(data[0])):
        count=0
        for i in range(len(data)):
            if data[i][j] == 0:
                count =1
        if count==len(data):
            print("column zero")
            lstIdx.append(j)
    data=np.delete(data,lstIdx,axis=1)               
    return data

data=drop_row(data)
data=drop_column(data)

print(data)

output:

[[0 1 8 4 0]
 [1 2 3 0 0]
 [3 2 3 0 5]]
  • Related