On Tue, Nov 3, 2009 at 4:46 PM, Steven D'Aprano email@example.com wrote:
def pick_two_cards(hand): assert isinstance(hand, (set, frozenset)) assert len(hand) == 5 return (hand.pick(), hand.pick())
Even if pick() chose random, you still might end up picking the same card twice. Is that really what you intended?
FWIW, I've been working on an extension module that supplies a "sortedset" type . In most ways, it's similar to a set except it's indexable like a list. The items are kept in sorted order, so index 0 is always the lowest item, index 1 is the next-to-lowest, etc. Because they're indexable, it's easy and efficient to retrieve random elements using the standard library's "random" module.
With the sortedset type, that function would become:
def pick_two_cards(hand): assert isinstance(hand, (set, frozenset)) assert len(hand) == 5 return random.choice(hand), random.choice(hand)
or if you want to avoid duplicates:
return random.sample(hand, 2)
Would something like that fit your needs?
 It's already implemented, along with sortedlist, weaksortedlist, and weaksortedset types. I'm just putting them through the paces in my own products before releasing them.
-- Daniel Stutzbach, Ph.D. President, Stutzbach Enterprises, LLC http://stutzbachenterprises.com