RE: Converting a string to an array?



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@xxxxxxxxxxx


-----Original Message-----
From: python-list-bounces+rgriswold=rioting.com@xxxxxxxxxx
[mailto:python-list-bounces+rgriswold=rioting.com@xxxxxxxxxx] On Behalf
Of Tim Chase
Sent: Thursday, January 12, 2006 12:20 PM
To: python-list@xxxxxxxxxx
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

.



Relevant Pages

  • Help in Spanish translation of the description of UDFs
    ... functions of minimum / maximum values among elements of an array column. ... GETALLWORDS- Inserts the words from a string into a global dimensioned ... WORDTRAN- Searches a character string for occurrences of a first word, ... ARRAYSUM- Returns the sum of all or a specified range of numeric (and/or ...
    (microsoft.public.fox.helpwanted)
  • Re: Check for Common character sequence ( I will pay)?
    ... Do I need to return an array? ... You need to identify character sequences of 3 or more characters that appear ... in more than one string. ... and test each 3-character sequence that results. ...
    (microsoft.public.dotnet.framework)
  • Re: Desirable Usage of Fortran Modules
    ... suppose we want to cast a character array as ... A function that returns a string ... array of double precision numbers: ...
    (comp.lang.fortran)
  • Re: Check for Common character sequence ( I will pay)?
    ... Yes you are returning an array of FoundString objects. ... in more than one string. ... This means that you have to identify sequences 1 character at a time, ... Again, obviously, if the 3-character sequence doesn't match, neither will ...
    (microsoft.public.dotnet.framework)
  • Re: character*1(28) and character*28
    ... One is a character string. ... Note, by the way, that your dmmy argument is an assumed-size array. ... Mostly the compiler does not know how long it ...
    (comp.lang.fortran)