Problem removing elements from an array: "can not delete array elements"


I have a problem to which I can not find a solution for more laps I give it. I have a program in which I generate a series of data and store them in a list. Later I have to remove some of the elements from the list so that its size is smaller. I always delete the first or last item in the list for which I use the "del" or "pop" functions.

To generate the list I want to work on, I call a function created by me that returns a list. Something like this:

y = generadatos(a1,a2,a3)
del y[0]

When it reaches the line with the "del" the program hangs. I tried to do it from the console by typing     from and [0] and it gives me the following error:


ValueError: can not delete array elements

However, if I open a new sheet and I think this simple code works without problems:

del a[0]

print a

If instead of using "from" use "pop" the same thing happens. I thought that maybe the list that returns the function that generates the data does something that I do not understand and that's why it does not let me delete elements.

Any ideas?

Thank you very much.


The "generadatos" function is this:

def generadatos(nst,p0st,ast,lbst,timesstrst,pvst,incremst):
xbigst = 0
xsmallst = 0

I0bigst = []
I0smallst = []

for ist in range(0,nst+1):
    pst = p0st + ast*ist

    pbigst = np.abs((pst*pvst)/(pst-pvst))
    psmallst = np.abs((pst*pvst)/(pst+pvst))

    limitpbigst = pbigst/lbst          
    limitpbigst =
    halflimitpbigst =

    limitpsmallst = psmallst/lbst          
    limitpsmallst =
    halflimitpsmallst =

    for jst in range(0,timesstrst):
        for kst in range(0,halflimitpbigst):
            xbigst = xbigst + 1

        for lst in range(halflimitpbigst,limitpbigst):
            xbigst = xbigst + 1

        for mst in range(0,halflimitpsmallst):
            xsmallst = xsmallst + 1

        for ost in range(halflimitpsmallst,limitpsmallst):
            xsmallst = xsmallst + 1            

limitpbig2st =
for qst in range (0,limitpbig2st):
    del I0bigst[-1]

limitpsmall2st =
for rst in range (0,limitpsmall2st):
    del I0smallst[-1]

if incremst > 0:
    aux1st = I0bigst[::]
    I0bigst = aux1st[::-1]
    aux1st = I0smallst[::]
    I0smallst = aux1st[::-1]

I0st = np.append(I0smallst,I0bigst)

return I0st'

At first I create empty lists in the I0 = [] style. Then I add data using 'I0.append (value) And finally I use the NumPy function     I0st = np.append (I0smallst, I0bigst)

return I0st
asked by Zhisi 22.11.2017 в 11:21

1 answer


The NumPy arrays are not resizable nor do they allow dynamic typing. This is because efficiency is sought, using compiled code of C below the most direct possible way. This implies that resizing an array implies whether or not to create a new array. This is the reason why del does not work.

There are two ways to remove elements from an array of NumPy, both involve creating a copy of the array and it is this copy that is returned (because as mentioned it could not be done inplace):

- Use the numpy.delete

    >>> import numpy as np

    # Eliminar el primer elemento:
    >>> a = np.array([1, 2, 3, 4])
    >>> a = np.delete(a, 0)
    >>> a
    array([2, 3, 4])

    # Eliminar el último elemento
    >>> a = np.array([1, 2, 3, 4])
    >>> a = np.delete(a, a.shape[0] - 1)
    >>> a
    array([1, 2, 3])

    # Eliminar el primero y el último:

    >>> a = np.array([1, 2, 3, 4])
    >>> a = np.delete(a, (0, a.shape[0] - 1))
    >>> a
    array([2, 3])

Receive three parameters:

  • The array as the first argument.

  • The index (or iterable indexes) of the element (s) to be eliminated as a second argument.

  • The axis on which it acts as a third party.


    Note: In the future (according to the developers themselves) np.delete will allow the use of negative indices, so we can change a = np.delete(a, a.shape[0] - 1) by a = np.delete(a,- 1) .

    - Use slicing techniques on the array. This option is usually more efficient, especially if we only delete the beginning or end of the array:

        >>> import numpy as np
        #Eliminar el primer elemento
        >>> a = np.array([1, 2, 3, 4])
        >>> a = a[1:]
        >>> a
        array([2, 3, 4])
        # Eliminar el último elemento
        >>> a = np.array([1, 2, 3, 4])
        >>> a = a[:-1]
        >>> a
        array([1, 2, 3])
        # Eliminar el primero y el último:
        >>> a = np.array([1, 2, 3, 4])
        >>> a = a[1:-1]
        >>> a
        array([2, 3])

    If you intended to use the element returned by list.pop , in this case you must first obtain the element through indexing and then apply the deletion with one of the two methods.

    It would also be possible to convert the array into a list or a queue (if the aggregation and elimination operations at the extremes are going to predominate), bearing in mind that there may be implications for the change of types and penalties in efficiency at move on to use a list or similar (in addition to not being able to use NumPy's own methods with it):

    >>> import numpy as np
    >>> a = np.array([1, 2, 3, 4])
    >>> a = a.tolist() 
    >>> a
    [1, 2, 3, 4]
    >>> a.pop()
    >>> a
    [1, 2, 3]
    >>> del(a[0])
    >>> a
    >>> [2, 3]

    If you are not interested in using NumPy arrays you should simply have your function return a list. If I0s is simply the result of concatenating two lists, as indicated in the code, just do just that:

    >>> I0smallst = [1]
    >>> I0bigst = [2,  5]
    >>> I0s = I0smallst + I0bigst
    >>> [1, 2, 5]
    answered by 22.11.2017 / 12:43