Created
February 23, 2017 16:10
-
-
Save jjsanderson/3b186ec0faa2937b6de61dabd75685a1 to your computer and use it in GitHub Desktop.
Less (?) elegant way of finding empty indices of source list, using itertools
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 | |
from itertools import compress, count | |
# Initial list of references | |
source = [0, 1, 3, 4, 0, 5, 7, 0] | |
print source | |
# Build list of indices of source where content is zero | |
empty = compress(count(), [not x for x in source]) | |
emptylist = list(empty) | |
print emptylist | |
# Select one of those indices at random | |
select = random.choice(emptylist) | |
print select | |
# Assign a value to the previously-empty element of the source list | |
source[select] = "TADA!" | |
print source |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment