Converting a string to an array?

Xavier Morel xavier.morel at masklinn.net
Thu Jan 12 17:51:15 EST 2006


Tim Chase wrote:
> 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
> 

Would

 >>> import random
 >>> s = "abcdefg"
 >>> data = list(s)
 >>> random.shuffle(data)
 >>> "".join(data)
'bfegacd'
 >>>

fit you better?



More information about the Python-list mailing list