undo a dictionary

mmm mdboldin at gmail.com
Thu Jul 31 11:29:39 EDT 2008


Gabriel,

I meant the latter, so this helps

> 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

But I got an error with 'for name, value in vars():'
RuntimeError: dictionary changed size during iteration

I think globals() has the same problem, but globals.items() works. I
will need to read the docs to learn why '.items()' works but the
changing global dictionary problem makes sense.  I assume I need to
use a non dynamic created list.

Sometimes I get the error about 'too many variables to unpack' (can
not consistently repeat the error however)

In any event, thanks for the suggestions, everyone.

Using a blank class for unpacking the dictionary makes the most sense,
for safety sake.
So you know the general issue is I want to switch between using
dictionaries for storing data items and simple variable names for
writing equations/formulas, So (a) and (b) below are preferred to (c)
for readability

(a)  straight forward equation

  y = b0 + b1*Age + b2*Size

(b)  Class version

  y = b0 + b1*x.Age + b2*x.Size

(c)  Dictionary version

  y = b0 + b1*dd.get('Age') + b2*dd.get('Size')




More information about the Python-list mailing list