diff Lib/random.py @ 37361:f05bd98d935d 2.4

Merge rev 43511 from the trunk. Another crack at bug #1460340: make random.sample(dict) work, this time by ugly brute force.
author Tim Peters <tim.peters@gmail.com>
date Sat, 01 Apr 2006 00:41:10 +0000
parents 60be3f39a72b
children 195a0d3eecd4
line wrap: on
line diff
--- a/Lib/random.py	Fri Mar 31 22:39:56 2006 +0000
+++ b/Lib/random.py	Sat Apr 01 00:41:10 2006 +0000
@@ -285,6 +285,15 @@
         large population:   sample(xrange(10000000), 60)
         """
 
+        # XXX Although the documentation says `population` is "a sequence",
+        # XXX attempts are made to cater to any iterable with a __len__
+        # XXX method.  This has had mixed success.  Examples from both
+        # XXX sides:  sets work fine, and should become officially supported;
+        # XXX dicts are much harder, and have failed in various subtle
+        # XXX ways across attempts.  Support for mapping types should probably
+        # XXX be dropped (and users should pass mapping.keys() or .values()
+        # XXX explicitly).
+
         # Sampling without replacement entails tracking either potential
         # selections (the pool) in a list or previous selections in a
         # dictionary.
@@ -302,7 +311,9 @@
         random = self.random
         _int = int
         result = [None] * k
-        if n < 6 * k:     # if n len list takes less space than a k len dict
+        if n < 6 * k or hasattr(population, "keys"):
+            # An n-length list is smaller than a k-length set, or this is a
+            # mapping type so the other algorithm wouldn't work.
             pool = list(population)
             for i in xrange(k):         # invariant:  non-selected at [0,n-i)
                 j = _int(random() * (n-i))
@@ -316,10 +327,10 @@
                     while j in selected:
                         j = _int(random() * n)
                     result[i] = selected[j] = population[j]
-            except (TypeError, KeyError):   # handle sets and dictionaries
+            except (TypeError, KeyError):   # handle (at least) sets
                 if isinstance(population, list):
                     raise
-                return self.sample(list(population), k)
+                return self.sample(tuple(population), k)
         return result
 
 ## -------------------- real-valued distributions  -------------------