Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #92469

Re: A basic dictionary question

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'sys': 0.05; '__name__': 0.07; 'bits': 0.07; 'definitions': 0.07; 'main()': 0.07; 'valueerror:': 0.07; 'subject:question': 0.08; 'defined.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'unpack': 0.09; 'python.': 0.11; 'def': 0.14; 'output': 0.15; "'__main__':": 0.16; "'address'": 0.16; '(key,': 0.16; '32,': 0.16; 'dictionary,': 0.16; 'dictionary.': 0.16; 'fields:': 0.16; 'iterates': 0.16; 'main():': 0.16; 'pairs': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'registers.': 0.16; 'wrote:': 0.16; 'fairly': 0.22; 'keys': 0.22; 'defined': 0.23; 'import': 0.24; '(most': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'url:dev': 0.27; 'dictionary': 0.29; 'key,': 0.29; 'value)': 0.29; 'values': 0.30; 'field,': 0.31; 'print': 0.31; 'code': 0.31; 'url:python': 0.33; 'class': 0.33; 'traceback': 0.33; 'this?': 0.34; 'file': 0.34; 'add': 0.34; 'could': 0.35; 'to:addr:python-list': 0.35; 'list': 0.35; 'too': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'should': 0.37; 'subject:: ': 0.37; 'received:org': 0.38; 'doing': 0.38; 'someone': 0.38; 'url:docs': 0.39; 'to:addr:python.org': 0.39; 'received:de': 0.40; 'some': 0.40; 'field': 0.60; 'different': 0.64; 'here': 0.66; '26,': 0.72; 'ranges': 0.76; "'range'": 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: A basic dictionary question
Date Thu, 11 Jun 2015 13:28:37 +0200
Organization None
References <41302A7145AC054FA7A96CFD03835A0A0B980FE0@EX10MBX02.EU.NEC.COM>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd827c.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.390.1434022136.13271.python-list@python.org> (permalink)
Lines 68
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1434022136 news.xs4all.nl 2897 [2001:888:2000:d::a6]:33692
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:92469

Show key headers only | View raw


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.


Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: A basic dictionary question Peter Otten <__peter__@web.de> - 2015-06-11 13:28 +0200

csiph-web