Skip to content

Instantly share code, notes, and snippets.

@tmerr
Created July 8, 2015 22:16
Show Gist options
  • Save tmerr/6f3620c7ab88acd828c4 to your computer and use it in GitHub Desktop.
Save tmerr/6f3620c7ab88acd828c4 to your computer and use it in GitHub Desktop.
def subs2(expr, substitutions):
"""
extends subs to conveniently support arrays
subs2(['a', [3,4]]) becomes subs([('a_0', 3), ('a_1', 4)])
"""
subslist = []
for name, val in substitutions:
if hasattr(val, '__getitem__') and not isinstance(val, str):
for i, vali in enumerate(val):
indexedname = '{}_{}'.format(name, i)
subslist.append((indexedname, vali))
else:
subslist.append((name, val))
return expr.subs(subslist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment