A basic dictionary question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

A basic dictionary question

David Aldrich-4
Hi

I am fairly new to Python.  I am writing some code that uses a dictionary to store definitions of hardware registers.  Here is a small part of it:

import sys

register = {
    'address'  : 0x3001c,
    'fields' : {
        'FieldA' : {
            'range' : (31,20),
        },
        'FieldB' : {
            'range' : (19,16),
        },
    },
    'width' : 32
};

def main():
    fields = register['fields']
    for field, range_dir in fields:         <== This line fails
        range_dir = field['range']
        x,y = range_dir['range']
        print(x, y)

if __name__ == '__main__':
    main()

I want the code to print the range of bits of each field defined in the dictionary.

The output is:

Traceback (most recent call last):
  File "testdir.py", line 32, in <module>
    main()
  File "testdir.py", line 26, in main
    for field, range_dir in fields:
ValueError: too many values to unpack (expected 2)

Please will someone explain what I am doing wrong?

Also I would like to ask how I could print the ranges in the order they are defined.  Should I use a different dictionary class or could I add a field to the dictionary/list to achieve this?

Best regards

David

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150611/1724352d/attachment.html>

Reply | Threaded
Open this post in threaded view
|

A basic dictionary question

Peter Otten
David Aldrich wrote:

> Hi
>
> I am fairly new to Python.  I am writing some code that uses a dictionary
> to store definitions of hardware registers.  Here is a small part of it:
>
> import sys
>
> register = {
>     'address'  : 0x3001c,
>     'fields' : {
>         'FieldA' : {
>             'range' : (31,20),
>         },
>         'FieldB' : {
>             'range' : (19,16),
>         },
>     },
>     'width' : 32
> };
>
> def main():
>     fields = register['fields']
>     for field, range_dir in fields:         <== This line fails
>         range_dir = field['range']
>         x,y = range_dir['range']
>         print(x, y)
>
> if __name__ == '__main__':
>     main()
>
> I want the code to print the range of bits of each field defined in the
> dictionary.
>
> The output is:
>
> Traceback (most recent call last):
>   File "testdir.py", line 32, in <module>
>     main()
>   File "testdir.py", line 26, in main
>     for field, range_dir in fields:
> ValueError: too many values to unpack (expected 2)
>
> Please will someone explain what I am doing wrong?

for key in some_dict:
    ...

iterates over the keys of the dictionary, for (key, value) pairs you need

for key, value in some_dict.items():
    ...

 
> Also I would like to ask how I could print the ranges in the order they
> are defined.  Should I use a different dictionary class or could I add a
> field to the dictionary/list to achieve this?

Have a look at collections.OrderedDict:

https://docs.python.org/dev/library/collections.html#collections.OrderedDict

If you don't care about fast access by key you can also use a list of
(key, value) pairs.




Reply | Threaded
Open this post in threaded view
|

A basic dictionary question

MRAB-2
In reply to this post by David Aldrich-4
On 2015-06-11 11:10, David Aldrich wrote:

> Hi
>
> I am fairly new to Python.  I am writing some code that uses a
> dictionary to store definitions of hardware registers.  Here is a small
> part of it:
>
> import sys
>
> register = {
>
>      'address'  : 0x3001c,
>
>      'fields' : {
>
>          'FieldA' : {
>
>              'range' : (31,20),
>
>          },
>
>          'FieldB' : {
>
>              'range' : (19,16),
>
>          },
>
>      },
>
>      'width' : 32
>
> };
>
> def main():
>
>      fields = register['fields']
>
>      for field, range_dir in fields:         <== This line fails
>
>          range_dir = field['range']
>
>          x,y = range_dir['range']
>
>          print(x, y)
>
> if __name__ == '__main__':
>
>      main()
>
> I want the code to print the range of bits of each field defined in the
> dictionary.
>
> The output is:
>
> Traceback (most recent call last):
>
>    File "testdir.py", line 32, in <module>
>
>      main()
>
>    File "testdir.py", line 26, in main
>
>      for field, range_dir in fields:
>
> ValueError: too many values to unpack (expected 2)
>
> Please will someone explain what I am doing wrong?
>
You're iterating over the keys. What you want is to iterate over
"fields.items()" which gives the key/value pairs.

> Also I would like to ask how I could print the ranges in the order they
> are defined.  Should I use a different dictionary class or could I add a
> field to the dictionary/list to achieve this?
>
Dicts are unordered. Try 'OrderedDict' from the 'collections' module.