[Tutor] filtering within a function.

Remco Gerlich scarblac@pino.selwerd.nl
Mon, 12 Mar 2001 23:37:50 +0100


On Mon, Mar 12, 2001 at 12:10:32PM -0500, Tesla Coil wrote:
> Revising cipher.py...
> http://www.lowerstandard.com/python/cipher.py
> to eliminate import of string, and discovered
> that the unstray function is bugged something
> awful (guess it usually gets the right input;
> this has gone unnoticed for a couple months).
> 
> Given:
> >>> foo = list('UPPERlowerUPPER12345')
> 
> This appears to work:
> 
> >>> uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
> >>> def f(x): return x in uppercase
> ... 
> >>> filter(f, foo)
> ['U', 'P', 'P', 'E', 'R', 'U', 'P', 'P', 'E', 'R']
> 
> Can't seem to make it work inside the function.

What do you mean? What did you do exactly, and how does it fail?

And why would you eliminate importing string when it has a perfectly fine
definition of uppercase?!

Hmmmm...

Did you do 'listinput = filter(f, listinput)' ?
In that case you only changed the local variable listinput, not the list it
referred to. Try

listinput[:] = filter(f, listinput)

(slice assignment does change the list)

-- 
Remco Gerlich