[Tutor] question about list

Bill Mill bill.mill at gmail.com
Fri Nov 12 04:01:43 CET 2004


Liam,

Sure. Sorry I was kinda short, I was in a rush earlier. Now, I got time. 

In [1]: a = [1,2,3,4,5,6]

In [2]: idx = [1,2,4]

Now, what we want to do is delete the first, second and fourth
elements (we'll let 1 be the 'zeroth' element, for convenience) of
'a'. I've renamed 'b' to 'idx', meaning index. When we delete the
first element of 'a', we have a problem - what does a look like now?

In [3]: del a[idx[0]]

In [4]: a
Out[4]: [1, 3, 4, 5, 6]

We've removed the first element, but the second and fourth elements
have now become the first and third elements. Thus, we need to
subtract one from every element of 'idx'. I accomplished this with a
list comprehension:

In [5]: idx = [elt-1 for elt in idx]

In [6]: idx
Out[6]: [0, 1, 3]

To read list comprehensions, read from the right. The first clause is
"for elt in idx" which works exactly like a for loop. The second
clause is "elt-1", which tells python to subtract one from the current
element. So, overall, the list comprehension tells python "make a list
composed of elt-1 for each elt in idx".
Does that make sense? Try playing around with them  in the interpreter
for a while. If you still don't understand what's going on (they're
hard), send a new message to the list and I'll try to give a more
detailed explanation of what's going on.

Now, to continue, when we do:

In [7]: del a[idx[1]]

In [8]: a
Out[8]: [1, 4, 5, 6]

We delete the correct element. However, again, the third element has
become the second element, so we have to update idx, and so on until
we're done.

Now, looking at this with more time has given me a better idea. If we
start removing entries from the back, instead of the front, the order
of the elements we want to remove won't change. Thus, this code:

a = [1,2,3,4,5,6]
idx = [1,2,4]
idx.sort()
idx.reverse()
for i in idx:
    del a[i]

Which works much more nicely, and very likely more efficiently, since
it doesn't have to iterate through the entire idx list every time it
deletes an element of a.

Does it make sense now? If not, feel free to drop me more questions.
I've been doing it for a while, so I tend to miss newbie mistakes.

Peace
Bill Mill

On Fri, 12 Nov 2004 15:26:14 +1300, Liam Clarke <cyresse at gmail.com> wrote:
> Heh,
> 
> If you don't mind, could you explain how that works?
> 
> Especially the b=[element..] part.
> 
> Day by day, I learn that I have so much more to learn. Sheesh.
> 
> Thanks,
> 
> Liam Clarke
> 
> 
> On Thu, 11 Nov 2004 19:59:09 -0500, Bill Mill <bill.mill at gmail.com> wrote:
> > Lin,
> >
> > b.sort()
> > for i in b:
> >     del a[i]
> >     b = [element-1 for element in b]
> >
> > Peace
> > Bill Mill
> >
> >
> >
> >
> > On Fri, 12 Nov 2004 07:24:50 +0800, Lin Jin <jinlin555 at msn.com> 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[i]
> > > >
> > > > 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
> > >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> 
> --
> 'There is only one basic human right, and that is to do as you damn well please.
> And with it comes the only basic human duty, to take the consequences.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list