On Thu, Jan 29, 2009 at 6:11 AM, garywood <[email protected]> wrote:
> I had a task in a book to pick 5 items from a list of 26 ensuring the
> items are not repeated
>
>
If the list is unique of 26 elements is guaranteed to be unique, simply:
>>> import random
>>> random.sample(list, 5)
['g', 'y', 'i', 'n', 'x']
If the list isn't unique:
>>> import random
>>> random.sample(set(list), 5)
['r', 'e', 'b', 'k', 'i']
If you want to combine them all into a single word as you do in your
example:
>>> import random
>>> ''.join(random.sample(set(list), 5))
'wmhsq'
--Stephen
--
http://mail.python.org/mailman/listinfo/python-list