1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
import re x = {'foo[bar]': 1, 'foo[1]': '2'} def get_dict(source, name): result = {} RE = re.compile(r'%s\[(\w+)\]' % name) print (source, source.keys()) for k in source.keys(): match_obj = RE.match(k) print (k, match_obj) if match_obj: result[match_obj.group(1)] = source[k] return result print get_dict(x, 'foo')
You can clone a snippet to your computer for local editing. Learn more.