Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

removing an instance of an object in python list

I Think this should work but its giving me an error. I have a list that contains objects of class node. I have two different lists

  1. open_list
  2. node_list.( they are not the same lengthwise, ordering wise)

When I find a specific node in the open_list I need to delete it from the node_list. I know that the lists have addresses to the objects stored in them

so when i try to do

removed = open_list.pop(min_index) 
node_list.remove(removed) 

it gives me an error saying

node_list.remove(removed)
ValueError: list.remove(x): x not in list

but the list just contains addresses that act like pointers right? it should match up the same addresses. i printed out the address of removed and the whole node_list (only 10 items for now don't fear) print out: (the last item in node_list matches the address of removed:

removed: <__main__.node instance at 0x0124A440>
node_list: [<__main__.node instance at 0x01246E90>, <__main__.node instance at 0x01246EE0>, <__main__.node instance at 0x0124A300>, <__main__.node instance at 0x0124A328>, <__main__.node instance at 0x0124A350>, <__main__.node instance at 0x0124A378>, <__main__.node instance at 0x0124A3A0>, <__main__.node instance at 0x0124A3C8>, <__main__.node instance at 0x0124A3F0>, <__main__.node instance at 0x0124A418>, <__main__.node instance at 0x0124A440>]

Thanks

follow-up Q

so I want to check if the node i want to remove exists in the node_list. when i looked up some simple list functions on http://docs.python.org/tutorial/datastructures.html

list.index(x) and remove.index(x) both give an error if the element is not in the list. this caused my program to stop running. to bypass this, can i use this statement before the .remove(): node in node_list i think the in checks to see if an element is part of a list and returns a bool. just double checking thanks,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is happening because what you understand as identifying features of two instances of your Node class, is not how python understands it.

The problem lies here. Suppose you asked python 5==5, python would return True. This is because python knows about ints. However, Node is a custom class that you defined, so you need to tell python when two Node objects are the same. Since you (probably) haven't, python defaults to comparing their locations in memory. Since two separate instances will be in two different memory locations, python will return False. If you are familiar with Java at all, this is like the difference between == and .equals(...)

In order to do this, go into your Node class and define the __eq__(self, other) method, where other is expected to be another instance of Node.

For example, if your nodes have an attribute called name and two nodes that have the same name are considered to be the same, then your __eq__ could look like this:

def __eq__(self, other):
    myName = self.name
    hisName = other.name
    if myName == hisName:
        return True
    else:
        return False

Of course, the more elegant way of writing that same function is:

def __eq__(self, other):
    return self.name == other.name

When this is done, your error should disappear

EDIT 1: In response to DSM's comment

class Node: pass
a = [Node(), Node()]
b = a[:]
b.remove(a.pop(0))

This will work. But upon closer inspection, it becomes evident that a[0] and b[0] are in fact the same object. This can be verified by calling id(a[0]) and comparing it with id(b[[0]) to confirm that they are indeed the same

EDIT 2: In response to the OP's follow up question (added to the original question as edit)

Yes, the object not existing in the list will cause an error which would normally stop program flow. This can be solved in either of the following two ways:

if x in my_list:
    my_list.remove(x)

OR

try:
    my_list.remove(x)
except:
    pass

The second method attempts to remove x from my_list and if that results in an error, ignores the error


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...