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


Groups > comp.lang.python > #92470

Re: A basic dictionary question

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
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; 'iterate': 0.09; 'unpack': 0.09; 'python.': 0.11; 'def': 0.14; 'output': 0.15; "'__main__':": 0.16; "'address'": 0.16; '32,': 0.16; 'dictionary.': 0.16; 'fields:': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'iterating': 0.16; 'key/value': 0.16; 'keys.': 0.16; 'main():': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'registers.': 0.16; 'wrote:': 0.16; 'fairly': 0.22; 'defined': 0.23; 'import': 0.24; 'header:In-Reply- To:1': 0.24; '(most': 0.24; 'header:User-Agent:1': 0.26; 'module.': 0.27; 'dictionary': 0.29; "skip:' 10": 0.30; 'values': 0.30; 'field,': 0.31; 'print': 0.31; 'code': 0.31; 'received:84': 0.32; 'class': 0.33; 'traceback': 0.33; 'this?': 0.34; 'file': 0.34; 'add': 0.34; 'gives': 0.35; 'could': 0.35; 'to:addr:python- list': 0.35; 'too': 0.36; 'should': 0.37; 'subject:: ': 0.37; 'doing': 0.38; 'someone': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'some': 0.40; 'field': 0.60; 'different': 0.64; 'here': 0.66; '26,': 0.72; 'ranges': 0.76; "'range'": 0.84
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=OoyysHLt c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=QrohdLjRRo4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=ZPWGPOyrnDPmeOvid_0A:9 a=QEXdDO2ut3YA:10
X-AUTH mrabarnett@:2500
Date Thu, 11 Jun 2015 12:29:25 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0
MIME-Version 1.0
To python-list@python.org
Subject Re: A basic dictionary question
References <41302A7145AC054FA7A96CFD03835A0A0B980FE0@EX10MBX02.EU.NEC.COM>
In-Reply-To <41302A7145AC054FA7A96CFD03835A0A0B980FE0@EX10MBX02.EU.NEC.COM>
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
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.391.1434022173.13271.python-list@python.org> (permalink)
Lines 77
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1434022173 news.xs4all.nl 2832 [2001:888:2000:d::a6]:34433
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:92470

Show key headers only | View raw


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.

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


Thread

Re: A basic dictionary question MRAB <python@mrabarnett.plus.com> - 2015-06-11 12:29 +0100

csiph-web