Created
June 7, 2018 23:45
-
-
Save jenkspt/beb65d02c958cd0648510f1716e6083e to your computer and use it in GitHub Desktop.
Randomly shuffles an iterator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def shuffle(iterator, buffer_size): | |
""" Uses a buffer to randomly shuffle items from an iterator """ | |
# Fill the buffer | |
buffer = []; cnt = 0 | |
item = next(iterator, None) | |
while not item is None and cnt < buffer_size: | |
buffer.append(item) | |
item = next(iterator, None) | |
cnt += 1 | |
random.shuffle(buffer) | |
print('beginning iteration') | |
while not item is None: | |
# Randomly select from the buffer | |
index = random.randint(0, cnt-1) | |
yield buffer[index] | |
# Replace the selected item | |
buffer[index] = item | |
item = next(iterator, None) | |
# Empty the buffer | |
while buffer: | |
yield buffer.pop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment