arange randomly words in a list

Tim Chase python.list at tim.thechases.com
Sat Oct 25 16:59:03 CEST 2008


> I have a list that looks like:
> 
> name = name1 name2 name3 name4 
> 
> and I would like to be able to arrange randomly this list, like:
> 
> name = name 2 name 1 name3 name4
> name = name4 name2 name1 name3
> ....
> 
> I have tried with random.shuffle, but still no good result
> 
> May I get an example?

I'm not sure what you mean by "still no good result" as using 
random.shuffle works quite nicely:

 >>> name = "name1 name2 name3 name4".split()
 >>> name
['name1', 'name2', 'name3', 'name4']
 >>> import random
 >>> random.shuffle(name)
 >>> name
['name1', 'name3', 'name4', 'name2']
 >>> print ' '.join(name)
name1 name3 name4 name2

which is exactly what you describe...

-tkc







More information about the Python-list mailing list