Deleting elements from numpy array is altering original list

78 Views Asked by At

I have a function which basically looks like this:

def calculatRatioList(self, inputList=None, listIndex=None):
    divisorArray = np.reshape(np.asarray(inputList[listIndex]), (-1))
    dividendArray = inputList
    ratioList = []
    # take element from inputList which will be the dividend
    for divisorIndex in range(len(divisorArray)):
        columnList = []
        del dividendArray[listIndex]
        for dividend_column in range(len(dividendArray)):
            quotient = 0
            dividend = np.asarray(dividendArray)[dividend_column][divisorIndex]
            divisor = float(divisorArray[divisorIndex])
            try:
                quotient = dividend / divisor
            except ZeroDivisionError:
                quotient = 0
            columnList.append(quotient)
        ratioList.append(columnList)
    return ratioList

I hope one can grasp the idea by just looking in the code above. Anyway, the idea briefly introduced is: take an array which is two dimensional (n,5), wherein n is any number. The listIndex parameter is the one representing the row, which shall be taken to the divisorArray, the dividendArray shall take the remaining elements from the inputList.

In order to delete the elements and go by like del dividendArray[listIndex]it always alters my inputList in the main.py either.

Can someone explain what this behaviour is good for? It annoys me and right now it seems more like a bug than something made with intuition.

What is a convenient way to delete elements in a numpy array, without going by a list.

I go by an input which looks like: inputList = [[a11, a12, a13, a14], [a21, a22, a23, a24], [a31, a32, a33, a34], [a41, a42, a43, a44]]

this input is assigned to dividendArray. After the line del dividendArray[listIndex] one can find inputList = [[a11, a12, a13, a14], [a21, a22, a23, a24], [a31, a32, a33, a34]]

0

There are 0 best solutions below