I want to remove an A element using a B array of IDs, given the specific scalar ID 'C'
In matlab I can do this:
A(B == C) = []
This is an example of my code:
boxes = [[1,2,20,20],[4,8,20,20],[8,10,40,40]]
boxIDs = [1,2,3]
IDx = 2
I want to delete the second box completely out of the list.
How can I do this in python? I have numpy.
without import numpy
you can pop
out the element. try:
boxes = [[1,2,20,20],[4,8,20,20],[8,10,40,40]]
IDx = 1
pop_element = boxes.pop(IDx)
the list boxes
now is [[1, 2, 20, 20], [8, 10, 40, 40]]
and pop_element
is [4, 8, 20, 20]
PS: in python indexes start from 0
instead of 1
.
You could use numpy indexing for that. You could find more information in the docs . For your case:
import numpy as np
boxes = np.array([[1,2,20,20],[4,8,20,20],[8,10,40,40]])
boxIDs = np.array([1,2,3])
IDx = 2
In [98]: boxes[boxIDs != IDx, :]
Out[98]:
array([[ 1, 2, 20, 20],
[ 8, 10, 40, 40]])
In plain-old-Python, I think you want to do this:
try:
helpindex = boxIDs.index(IDx)
del boxes[index], boxIDs[index]
except ValueError:
# Already deleted
pass
Note that if you are relying on boxIDs
to be "parallel" with boxes
, you have to make sure you keep them parallel by deleting from both.
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.