[issue1551113] random.choice(setinstance) fails

2013-05-10 Thread Raymond Hettinger
Raymond Hettinger added the comment: Sorry, this is still rejected for the reasons that Tim mentioned. -- ___ Python tracker ___ ___

[issue1551113] random.choice(setinstance) fails

2013-05-07 Thread wim glenn
wim glenn added the comment: How about if isinstance(seq, collections.Sequence): # do it the usual way .. else: return choice(list(seq)) -- ___ Python tracker ___ ___

[issue1551113] random.choice(setinstance) fails

2013-05-07 Thread wim glenn
wim glenn added the comment: The implementation suggested by the OP def choice(self, seq): """Choose a random element from a non-empty sequence.""" idx = int(self.random() * len(seq)) try: result = seq[idx] # raises IndexError if seq is empty except TypeError:

[issue1551113] random.choice(setinstance) fails

2009-03-26 Thread Tim Peters
Changes by Tim Peters : -- ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/opt

[issue1551113] random.choice(setinstance) fails

2009-03-26 Thread Tim Peters
Tim Peters added the comment: The CPython set/dict implementation does not guarantee "minimal constant density", so "quite easy" doesn't apply in reality. For example, a set that once contained a million elements may still contain a million /slots/ for elements after all but one of the elements

[issue1551113] random.choice(setinstance) fails

2009-03-26 Thread Tim Peters
Tim Peters added the comment: The CPython set/dict implementation does not guarantee "minimal constant density", so "quite easy" doesn't apply in reality. For example, a set that once contained a million elements may still contain a million /slots/ for elements after all but one of the elements

[issue1551113] random.choice(setinstance) fails

2009-03-26 Thread Rob Renaud
Rob Renaud added the comment: I found this via google search when disappointed that random.choice raised an exception rather than returned a random item in the set. It's quite easy to implement random.choice for sets/dicts in O(1) expected time from the C implementation as long as the set/dict