Serhiy Storchaka <[email protected]> added the comment:
Possible implementation:
from itertools import islice as _islice
def reservoir_sample(self, population, k):
if k < 0:
raise ValueError("Sample is negative")
it = iter(population)
result = list(_islice(it, k))
if len(result) < k:
raise ValueError("Sample larger than population")
self.shuffle(result)
randbelow = self._randbelow
for i, x in enumerate(it, k+1):
j = randbelow(i)
if j < k:
result[j] = x
return result
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue37682>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com