undo a dictionary

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Jul 30 21:07:31 EDT 2008


En Wed, 30 Jul 2008 16:14:31 -0300, mmm <mdboldin at gmail.com> escribi�:

>> > And for that matter a way to create a
>> > dictionary from a set of variables (local or global).
>>
>> You have to be more specific: there are {} displays and dict(args) call
>> and other methods.  Read the manual.
>
> 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

dict(x1=1, x2=20, x3=33) does the same thing.

Or, do you mean you already have those names and values, perhaps mixed  
with a lot more names, and want to extract only those starting with "x"  
and following with a number?

result = {}
for name, value in vars(): # or locals().items(), or globals().items(), or  
vars(some_module)
   if name[0]=='x' and name[1:].isdigit():
     result[name] = value

-- 
Gabriel Genellina




More information about the Python-list mailing list