I have a problem that I can't find a solution no matter how many times I give it. I have a program where I generate a series of data and store it in a list. Later on I have to remove some of the list items to make their size smaller. I always remove the first or last element of the list for which I use the "del" or "pop" functions.
To generate the list that I want to work on, I call a function that I created and that returns a list. Something like that:
y = generadatos(a1,a2,a3)
del y[0]
When it reaches the line with the "del" the program hangs. I have tried to do it from the console by typing del y[0] and it gives me the following error:
ValueError: cannot delete array elements
However if I open a new sheet and create this simple code it works without problems:
a=[1,2,3,4]
del a[0]
print a
If instead of using "del" I use "pop" the same thing happens. I have thought that perhaps the list that the function that generates the data returns to me does something that I do not understand and that is why it does not let me delete elements.
Any ideas?
Thank you very much.
Edit1:
The "generated" 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 = np.int(limitpbigst)
halflimitpbigst = np.int(limitpbigst/2)
limitpsmallst = psmallst/lbst
limitpsmallst = np.int(limitpsmallst)
halflimitpsmallst = np.int(limitpsmallst/2)
for jst in range(0,timesstrst):
for kst in range(0,halflimitpbigst):
I0bigst.append(1)
xbigst = xbigst + 1
for lst in range(halflimitpbigst,limitpbigst):
I0bigst.append(0)
xbigst = xbigst + 1
for mst in range(0,halflimitpsmallst):
I0smallst.append(1)
xsmallst = xsmallst + 1
for ost in range(halflimitpsmallst,limitpsmallst):
I0smallst.append(0)
xsmallst = xsmallst + 1
limitpbig2st = np.int((pbigst/2)/lbst)
for qst in range (0,limitpbig2st):
del I0bigst[-1]
limitpsmall2st = np.int((psmallst/2)/lbst)
for rst in range (0,limitpsmall2st):
del I0smallst[-1]
if incremst > 0:
aux1st = I0bigst[::]
I0bigst = aux1st[::-1]
else:
aux1st = I0smallst[::]
I0smallst = aux1st[::-1]
I0st = np.append(I0smallst,I0bigst)
return I0st`
At first I create empty lists like I0 = []
. Then I add data using `I0.append(value) And at the end I use the NumPy function I0st = np.append(I0smallst,I0bigst)
return I0st
NumPy arrays are not resizable nor do they support dynamic typing. This is so because efficiency is sought, using compiled C code below in the most direct way possible. This implies that resizing an array implies whether or whether to create a new array. This is the reason why it
del
doesn't work.There are two ways to remove elements from a NumPy array, both involve creating a copy of the array and it is this copy that is returned (because, as mentioned, inplace cannot be done):
- use the method
numpy.delete
Receives three parameters:
The array as the first argument.
The index (or iterable with the indices) of the element(s) to remove as the second argument.
The axis on which it acts as a third party.
- Use slicing techniques on the array. This option is usually more efficient, especially if we only remove from the beginning or the end of the array:
If you intended to use the element returned by
list.pop
, in this case you must first get the element by indexing and then apply the removal with one of the two methods.It would also be possible to convert the array to a list or a queue (if add and delete operations at the ends are going to dominate), keeping in mind that there may be implications for changing types and efficiency penalties when switching to using a list or similar (besides not being able to use NumPy's own methods with it):
If you don't care about using NumPy arrays you should just make your function return a list. If
I0s
it is simply the result of concatenating two lists, as you seem to indicate in the code, it is enough to do just that: