Skip to content

Instantly share code, notes, and snippets.

@RickyCook
Created July 25, 2016 14:51
Show Gist options
  • Select an option

  • Save RickyCook/7e54a5a7e934bee8f9618c4acd72f649 to your computer and use it in GitHub Desktop.

Select an option

Save RickyCook/7e54a5a7e934bee8f9618c4acd72f649 to your computer and use it in GitHub Desktop.
def i_to_w(indexes, wordlist):
"""
Examples:
>>> wl = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> i_to_w([0, 0, 0], wl)
['a', 'b', 'c']
>>> i_to_w([1, 1, 1], wl)
['b', 'c', 'd']
>>> i_to_w([3, 3, 3], wl)
['d', 'e', 'f']
>>> i_to_w([3, 4, 4], wl)
['d', 'f', 'g']
>>> i_to_w([1, 0, 0], wl)
['b', 'a', 'c']
"""
return list(i_to_w_gen(indexes, wordlist.copy()))
def i_to_w_gen(indexes, wordlist_mut):
for index in indexes:
yield wordlist_mut[index]
wordlist_mut.pop(index)
def w_to_i(words, wordlist):
"""
Examples:
>>> wl = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> w_to_i(['a', 'b', 'c'], wl)
[0, 0, 0]
>>> w_to_i(['b', 'c', 'd'], wl)
[1, 1, 1]
>>> w_to_i(['d', 'e', 'f'], wl)
[3, 3, 3]
>>> w_to_i(['d', 'f', 'g'], wl)
[3, 4, 4]
>>> w_to_i(['b', 'a', 'c'], wl)
[1, 0, 0]
"""
return list(w_to_i_gen(words, wordlist.copy()))
def w_to_i_gen(words, wordlist_mut):
for word in words:
idx = wordlist_mut.index(word)
yield idx
wordlist_mut.pop(idx)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment