简体   繁体   中英

How to delete an unknown number of numpy array indices?

I have a numpy array in which I'd like to delete any zero values in the data. These can occur more than once so I can't use remove(), I can't use pop() because I don't want the repeated value, and I can't use del because the numpy array is immutable. I saw an example online that can be seen below, which would work but (since the data that would be used is dynamic and changing) it doesn't in this case since there's more than more index to eliminate. Basically I need a copy of the same array but an n amount of indices removed.

#What was seen online
b = np.delete(a, [2,3,6])

#My current code
for i in range(487):
    if initial[i] == 0:
        newInitial[i] = np.delete(initial,i)
        newSum = np.delete(summary,[0,i])
   
    else:
        pass

for i in range(487):
    if final[i] == 0:]
        newFinal = np.delete(final,i)
        newSum = np.delete(summary,[-1,i])
    
    else:
        pass
    
for i in range(3):
    for j in range(487):
        if middle[i,j] == 0:
            newMid = np.delete(middle,[i,j])
            newSum = np.delete(summary,[i+1,j])
            
        else:
            pass
        
for i in range(487):
    if summary[0,i] == 0:
        newSum = np.delete(summary,[0,i])
        newInitial = np.delete(initial,i)
   
    else:
        pass

for i in range(487):
    if summary[-1,i]:
        newSum = np.delete(summary,[-1,i])
        newFinal = np.delete(final,i)
    
    else:
        pass
    
for i in range(3):
    for j in range(487):
        if summary[i+1,j] == 0:
            newSum = np.delete(summary,[i+1,j])
            newMid = np.delete(middle,[i,j])

    else:

maybe if you use the command del

for example:

list = [1, 2, 3, 4, 5] # declarate a numeric list

del list[1] # delete the element number 1 of the array list

del list[0:3] # delete all this elements of the interval 0 → 3 of the array (star:end)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM