Converting a string to an array?

Bryan Olson fakeaddress at nowhere.org
Fri Jan 13 05:52:44 EST 2006


Tim Chase wrote:
> While working on a Jumble-esque program, I was trying to get a string 
> into a character array.  Unfortunately, it seems to choke on the following
> 
>     import random
>     s = "abcefg"
>     random.shuffle(s)
> 
> returning
> 
>   File "/usr/lib/python2.3/random.py", line 250, in shuffle
>     x[i], x[j] = x[j], x[i]
>   TypeError: object doesn't support item assignment

Yes, Python has had that same problem elsewhere, notably the
mutating methods 'sort' and 'reverse'. Those problems are now
reasonably well solved. What's really neat is that the names
of the new methods: 'sorted' and 'reversed', are adjectives
describing the return values we want, where 'sort' and
'reverse' are verbs, calling for procedures to be performed.


For sorting, we had the procedure 'sort', then added the pure
function 'sorted'. We had a 'reverse' procedure, and wisely
added the 'reversed' function.

Hmmm... what we could we possible do about 'shuffle'?


-- 
--Bryan



More information about the Python-list mailing list