Sorted list - how to change it

Nick Craig-Wood nick at craig-wood.com
Thu Nov 9 09:30:06 EST 2006


Tim Chase <python.list at tim.thechases.com> wrote:
>  Just a caveat from past experience...while the OP was talking 
>  about lists, for future reference random.shuffle() chokes on 
>  strings (and possibly tuples).  It requires the ability to edit 
>  the target/parameter in place...a functionality that strings 
>  don't provide.

You can always use array.array('c'), eg

   >>> import random
   >>> import array
   >>> L = array.array('c')
   >>> L.fromstring("hello")
   >>> L
   array('c', 'hello')
   >>> random.shuffle(L)
   >>> L
   array('c', 'elohl')
   >>> L.tostring()
   'elohl'

Which is some way towards a mutable string...

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list