Skip to content

Instantly share code, notes, and snippets.

@mynameisfiber
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save mynameisfiber/9a0902b22da2d5f409f8 to your computer and use it in GitHub Desktop.

Select an option

Save mynameisfiber/9a0902b22da2d5f409f8 to your computer and use it in GitHub Desktop.
# Why does this work
def split_chars(words, chars):
itr = (words,)
for c in chars:
itr = tuple(chunk for word in itr for chunk in word.split(c))
return list(itr)
# but this not work
def split_chars_broken(words, chars):
itr = (words,)
for c in chars:
itr = (chunk for word in itr for chunk in word.split(c))
return list(itr)
>>> split_chars("assdf,asdfadsf.sadfadsf,asdf.asdf", ".,")
['assdf', 'asdfadsf', 'sadfadsf', 'asdf', 'asdf']
>>> split_chars_broken("assdf,asdfadsf.sadfadsf,asdf.asdf", ",.")
['assdf,asdfadsf', 'sadfadsf,asdf', 'asdf']
>>> split_chars_broken("assdf,asdfadsf.sadfadsf,asdf.asdf", ".,")
['assdf', 'asdfadsf.sadfadsf', 'asdf.asdf']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment