exercise: partition a list by equivalence

John Machin sjmachin at lexicon.net
Sat Feb 19 00:57:59 EST 2005


John Lenton wrote:
> On Fri, Feb 18, 2005 at 04:52:46PM -0800, John Machin wrote:
> > > > needs "if rev[first] == rev[second]: continue" here
> > >
> > > an 'is' is enough, and better.
> >
> > Good point. You're redeeming yourself :-)
>
> this, together with you saying that it is hard to explain, makes me
> think that you aren't comfortable thinking of lists as mutable
> objects.

How so? There is no connection between is/== and mutability. Let me
amplify: The point about 'is' is a good one, and aids your redemption
after your failure to have adequate guards caused your algorithm not to
work.

>
>
> > > what is magic about it? is it really that horrible?
> >
> > Try explaining to the newbies over on the tutor list how despite
"res"
> > only ever *explicitly* having little bits like [3, 4] appended to
it,
> > it (or more properly the thing to which it refers) is actually
> > festering and growing and being mutated under the surface until at
the
> > finale it bursts out dripping slime just like the creature from the
> > black lagoon ...
>
> understanding why that works, and why it is 'is' and not '==', are
> both part of the same thing.

What same thing is that?

> Lists are mutable, and you can mutate
> them, and they mutate. Unless you actually write code that uses the
> fact you will forget it, and it will bite you. Of course, don't use
it
> just for the heck of it, but that creature you dismiss as a
> slime-dripping mutation is actually quite useful.

You are confusing mutability of lists (without which they would be
called tuples!) with my point that the 'res' list was having its
contents fiddled with implicitly through other "pointers".

>
> While I'm at being unpolite, do you really think this code was harder
> to understand than the code posted by anton, using numarray?

I only read it as far as the bit where it creating a matrix of size
O(N**2) -- in my app N can be over a million so I lost interest
rapidly.

>
> And, of course, if this code were for anything non-throw-awayable,
> there would've been quite a bit of explaining going on between those
> lines of code.
>
Not of course, but of necessity.

> Ok, now back to being polite :)

Welcome back.




More information about the Python-list mailing list