undo a dictionary

Alan Franzoni alan.franzoni.blahblah at example.com.invalid
Wed Jul 30 15:32:04 EDT 2008


mmm was kind enough to say:

> My desire is to take a set of data items in an alpha-numeric range and
> oput them into a dictionary
> 
> i.e.,
> x1=1
> x2=20
> x3=33
> 
> to yield  the dictionary
> 
> { 'x1':1, 'x2':20, 'x3':33 }
> 
> without having to type in as above but instead invoke a function
> 
> maybe with syntax of
> 
> dd=make_dict('x1--x99')

you'll need to pass the locals() or globals() manually. Can't be done
(cleanly) otherwise. Check the following code:

import re

x1=1
x2=20
x3=30
q6=40

def make_dict(regex, nsmapping):
    output_dict = {}
    compiled_regex = re.compile(regex)
    for key, value in nsmapping.items():
        if compiled_regex.match(key):
            output_dict[key] = value

    return output_dict




d = make_dict("x[123]", locals())

print d
        

    
    

-- 
Alan Franzoni <alan.franzoni.xyz at gmail.com>
-
Remove .xyz from my email in order to contact me.
-
GPG Key Fingerprint:
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E



More information about the Python-list mailing list