Converting a string to an array?
Ron Griswold
RGriswold at Rioting.com
Thu Jan 12 18:00:36 EST 2006
Does this do what you are looking for?
>>> s = 'abcdefg';
>>> a = [];
>>> a += s;
>>> a;
['a', 'b', 'c', 'd', 'e', 'f', 'g']
Ron Griswold
Character TD
R!OT Pictures
rgriswold at rioting.com
-----Original Message-----
From: python-list-bounces+rgriswold=rioting.com at python.org
[mailto:python-list-bounces+rgriswold=rioting.com at python.org] On Behalf
Of Tim Chase
Sent: Thursday, January 12, 2006 12:20 PM
To: python-list at python.org
Subject: Converting a string to an array?
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
The closest hack I could come up with was
import random
s = "abcdefg"
a = []
a.extend(s)
random.shuffle(a)
s = "".join(a)
This lacks the beauty of most python code, and clearly feels like
there's somethign I'm missing. Is there some method or function
I've overlooked that would convert a string to an array with less
song-and-dance? Thanks,
-tim
--
http://mail.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list