[Tutor] Acting on objects in a list - how to make the
change permanent?
Kent Johnson
kent_johnson at skillsoft.com
Tue Sep 28 04:13:05 CEST 2004
Adam,
You are confusing a "list of things that should be deleted" with the list
from which they should be deleted.
For example suppose I have the list [1, 2, 3, 4, 5]:
>>> a=[1, 2, 3, 4, 5]
>>> a
[1, 2, 3, 4, 5]
Now I get a list of things to delete from a:
>>> d=[1, 3]
Deleting something from d will not be helpful!
>>> d.remove(1)
>>> a
[1, 2, 3, 4, 5]
>>> d
[3]
I have to remove from a:
>>> d=[1, 3]
>>> for i in d:
... a.remove(i)
...
>>> d
[1, 3]
>>> a
[2, 4, 5]
OK, now in your real code, you have a nested structure of lists - a
magazine collection has titles which have issues which have articles. So
it's not so simple, when you have just the list d, to figure out what list
to delete from - it could be any of the issues in any of the titles.
The solution is to remember which list the item to be deleted occurs in.
Say we have two lists:
>>> a=[1, 2, 3, 4, 5]
>>> b=[3, 4, 5, 6]
If I tell you to remove item 3, what will you do? But if I tell you to
remove item 3 from list b, you know what to do. So the list of things to
delete has to include information about the list to delete from. Say we
want to remove 3 from b and 4 from a:
>>> d=[ (3, b), (4, a) ]
Now d is a list of tuples. Each tuple tells me an item to delete from a
specific list. Here's how to remove them:
>>> for val, lst in d:
... lst.remove(val)
...
>>> a
[1, 2, 3, 5]
>>> b
[4, 5, 6]
You need to do something like this. Your list of things to remove has to
include a reference to the list from which to remove it.
Kent
At 03:01 PM 9/27/2004 +0100, Adam Cripps wrote:
>My python project (http://savannah.nongnu.org/projects/newmag) is
>teaching me a fair bit about how to write python code. However, I'm
>still a long way away from being remotely good, as some of my recent
>posts have shown.
>
>I'm working on deleting instance objects and I've created a list of
>objects which is then piped through a menu to allow the user to choose
>which one is deleted.
>
>The basic structure of the code is this:
>
> * choose_what_to_edit - creates a list which calls return_object_list
> * return_object_list - return_object_list -chooses what level
>the user wants to select an object
> * choosetitle - selects a title
> * chooseissue - if necssary selects an issue from the
> selected title
> * choosearticle - and if nescessary, selects an article
>from the selected issue.
>
>Basically, from this code, I get a list, which has objects within it.
>If I alter the objects within this list, how do I make them permanent?
>I'm currently trying to delete an item/object of the list the command
>del returned_list[choice] - however, this doesn't seem to be a
>permanent change. For the full code listing, please see the CVS
>project page.
>
>Am I right in thinking that the list of objects will be referenced
>through to the actual instances, and if so, why aren't they being
>deleted? I've read some of the python docs, but can't see the solution
>here and would appreciate a more informed/specialised approach.
>
>TIA.
>Adam
>_______________________________________________
>Tutor maillist - Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list