Skip to content

Instantly share code, notes, and snippets.

@cameronp98
Last active December 30, 2015 17:49
Show Gist options
  • Save cameronp98/7863856 to your computer and use it in GitHub Desktop.
Save cameronp98/7863856 to your computer and use it in GitHub Desktop.
Returns all strings from the supplied iterable in which the supplied characters are present with the same number of occurrences
def string_match(data, chars):
for item in data:
stack = list(item)
[stack.pop(stack.index(c)) for c in chars if c in stack]
if len(stack) == len(item) - len(chars):
yield item
data = ["happy", "hand", "charming", "this", "horn", "jamming", "choppy"]
chars = "pp"
print(list(string_match(data, chars)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment