[Tutor] permutations, patterns, and probability
Kent Johnson
kent37 at tds.net
Wed Feb 2 04:38:49 CET 2005
kevin parks wrote:
> Tremendously helpful!!!! One question though. How can i pluck a unique
> item from my exhaustive list of permutations without repeats making sure
> that each one is used once? Like filling a bag, shaking it, and then
> picking from the bag and removing that item from the bag so it isn't
> used again....
Use random.shuffle() to 'shake' the list. Then use pop() to remove an item:
>>> import random
>>> l=range(10)
>>> random.shuffle(l)
>>> l
[4, 5, 0, 8, 9, 6, 2, 1, 7, 3]
>>> l.pop()
3
>>> l.pop()
7
>>> l.pop()
1
>>> l
[4, 5, 0, 8, 9, 6, 2]
Kent
More information about the Tutor
mailing list