[Tutor] a question about list

Lloyd Kvam pythonTutor at venix.com
Fri Nov 12 00:17:50 CET 2004


a=[a,b,c,d,e,f,g]
b=[1,2,4]	# so you want to delete b,c,e
del a[1]
a=[a,c,d,e,f,g]
del a[2]
a=[a,c,e,f,g]
del a[4]
a=[a,c,e,f]

When you lay it out, you can see what is happening.  As the list gets
smaller, the index for an item changes.  The best solution depends upon
what you are really trying to do.

You could make list_a into a dictionary with the index numbers as keys
to the values.  This gets around the renumbering.

You could be careful to delete from list_a using the largest remaining
index value.  This would avoid changing the index numbers of your future
deletions.  So long as all of the index number values are known before
you start deleting, that is a viable strategy.


On Thu, 2004-11-11 at 17:43, Lin Jin wrote:
> i am new to python.i have a question about list.if i have two list:
> a=[a,b,c,d,e,f,g]
> b=[1,2,4]
> and i want to remove the element of a using b,that is i want a=[a,d,f,g],my 
> code is like this:
> >>>for i in b:
> >>>    del a[b]
> 
> but it is not working, it can't remove the correct items.what should i do 
> to make it correct?thx
> 
> _________________________________________________________________
> 免费下载 MSN Explorer:   http://explorer.msn.com/lccn/  
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp



More information about the Tutor mailing list