In Python, how can I remove an object from array of objects? Like this:
在Python中,如何从对象数组中删除对象?是这样的:
x = object()
y = object()
array = [x,y]
# Remove x
I've tried array.remove()
but it only works with a value, not a specific location in the array. I need to be able to delete the object by addressing its position(remove array[0]
)
我尝试过array.remove(),但它只对一个值起作用,而不是数组中的特定位置。我需要能够通过寻址对象的位置来删除它(删除数组[0])
5 个解决方案
#1
59
In python there are no arrays, lists are used instead. There are various ways to delete an object from a list:
在python中没有数组,而是使用列表。从列表中删除对象有多种方法:
my_list = [1,2,4,6,7]
del my_list[1] # Removes index 1 from the list
print my_list # [1,4,6,7]
my_list.remove(4) # Removes the integer 4 from the list, not the index 4
print my_list # [1,6,7]
my_list.pop(2) # Removes index 2 from the list
In your case the appropriate method to use is pop, because it takes the index to be removed:
在你的情况下,适当的方法是pop,因为它需要删除索引:
x = object()
y = object()
array = [x, y]
array.pop(0)
# Using the del statement
del array[0]
#2
5
del array[0]
where 0
is the index of the object in the list (there is no array in python)
其中0是列表中对象的索引(python中没有数组)
#3
1
If you know the array location you can can pass it into itself. If you are removing multiple items I suggest you remove them in reverse order.
如果你知道数组的位置,你可以把它传递给它自己。如果您正在删除多个项目,我建议您以相反的顺序删除它们。
#Setup array
array = [55,126,555,2,36]
#Remove 55 which is in position 0
array.remove(array[0])
#4
1
If you want to remove multiple object from a list. There are various ways to delete an object from a list
如果要从列表中删除多个对象。有多种方法可以从列表中删除对象
Try this code. a is list with all object, b is list object you want to remove.
试试这个代码。a是带有所有对象的列表,b是要删除的列表对象。
example :
例子:
a = [1,2,3,4,5,6]
b = [2,3]
for i in b:
if i in a:
a.remove(i)
print(a)
the output is [1,4,5,6]
I hope, it will work for you
输出是[1,4,5,6]我希望,它对你有用。
#5
-1
if you wanna remove the last one just do your_list.pop(-1)
if you wanna remove the first one your_list.pop(0)
or any index you wish to remove
如果你想要删除最后一个,只需执行your_list.pop(-1)如果你想删除第一个你的_list.pop(0)或任何你想要删除的索引
#1
59
In python there are no arrays, lists are used instead. There are various ways to delete an object from a list:
在python中没有数组,而是使用列表。从列表中删除对象有多种方法:
my_list = [1,2,4,6,7]
del my_list[1] # Removes index 1 from the list
print my_list # [1,4,6,7]
my_list.remove(4) # Removes the integer 4 from the list, not the index 4
print my_list # [1,6,7]
my_list.pop(2) # Removes index 2 from the list
In your case the appropriate method to use is pop, because it takes the index to be removed:
在你的情况下,适当的方法是pop,因为它需要删除索引:
x = object()
y = object()
array = [x, y]
array.pop(0)
# Using the del statement
del array[0]
#2
5
del array[0]
where 0
is the index of the object in the list (there is no array in python)
其中0是列表中对象的索引(python中没有数组)
#3
1
If you know the array location you can can pass it into itself. If you are removing multiple items I suggest you remove them in reverse order.
如果你知道数组的位置,你可以把它传递给它自己。如果您正在删除多个项目,我建议您以相反的顺序删除它们。
#Setup array
array = [55,126,555,2,36]
#Remove 55 which is in position 0
array.remove(array[0])
#4
1
If you want to remove multiple object from a list. There are various ways to delete an object from a list
如果要从列表中删除多个对象。有多种方法可以从列表中删除对象
Try this code. a is list with all object, b is list object you want to remove.
试试这个代码。a是带有所有对象的列表,b是要删除的列表对象。
example :
例子:
a = [1,2,3,4,5,6]
b = [2,3]
for i in b:
if i in a:
a.remove(i)
print(a)
the output is [1,4,5,6]
I hope, it will work for you
输出是[1,4,5,6]我希望,它对你有用。
#5
-1
if you wanna remove the last one just do your_list.pop(-1)
if you wanna remove the first one your_list.pop(0)
or any index you wish to remove
如果你想要删除最后一个,只需执行your_list.pop(-1)如果你想删除第一个你的_list.pop(0)或任何你想要删除的索引