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


Groups > comp.lang.python > #68472

Re: Ordering in the printout of a dictionary

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'explicitly': 0.05; 'output': 0.05; 'iterate': 0.09; 'so?': 0.09; 'strings.': 0.09; 'typed': 0.09; 'types:': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '11:32': 0.16; 'exception:': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'printout': 0.16; 'str()': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; 'work,': 0.20; '>>>': 0.22; 'input': 0.22; 'cc:addr:python.org': 0.22; 'skip:% 10': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; 'this:': 0.26; 'least': 0.26; 'certain': 0.27; 'header:In-Reply-To:1': 0.27; 'skip:p 30': 0.29; 'am,': 0.29; 'originally': 0.30; 'message-id:@mail.gmail.com': 0.30; '(which': 0.31; "skip:' 10": 0.31; '"",': 0.31; 'keys': 0.31; 'values.': 0.31; 'file': 0.32; 'probably': 0.32; '(i.e.': 0.33; '(most': 0.33; 'subject:the': 0.34; 'could': 0.34; 'display': 0.35; '(2)': 0.35; 'form.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'consistent': 0.36; 'easiest': 0.38; 'mapping': 0.38; 'stable': 0.38; 'fact': 0.38; 'recent': 0.39; 'explain': 0.39; 'skip:p 20': 0.39; 'kindly': 0.61; 'simply': 0.61; 'simple': 0.61; "you're": 0.61; 'first': 0.61; 'mar': 0.68; 'where:': 0.68; 'phenomenon': 0.84; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=EJ9a50NfNfs90H1viFxpQpyVotNh8ziXsgHoXKUX7Xk=; b=rD9mqJ0JrVpu4FnN/+HGKczzFd3hq2tnfoV6JoZBO0P+RHmm/O1rI3S4ErENTP0Gkw kZeDT8Gno6LKG1NT5saQ0BECW85cEsK18NCFUp+VVxIyEtbmQrUO/i3eH2rXueWTLMOa EZhJRbwABUxQyW/S76EIncl1lgJM1mfTd+sj7zoT83J+LgHnB09kY0oCpb/yicZB46IO UU37Ljt8C+dPUiWwPKu0lVEoI+G+15909PmB7lnWBnkmria4+8LT8rYY5m+atSaZxtI+ Cukpoi1n9LnkGLFpgIT4oPJVasU9eIPoFeH6fhDeQN0HKVqQXFGRgQZkIKxB1woHf19h Yd6g==
MIME-Version 1.0
X-Received by 10.68.235.6 with SMTP id ui6mr28239000pbc.45.1395103684103; Mon, 17 Mar 2014 17:48:04 -0700 (PDT)
In-Reply-To <lg8462$fnk$1@news.albasani.net>
References <lg8462$fnk$1@news.albasani.net>
Date Tue, 18 Mar 2014 11:48:04 +1100
Subject Re: Ordering in the printout of a dictionary
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.8231.1395103693.18130.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1395103693 news.xs4all.nl 2883 [2001:888:2000:d::a6]:57932
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:68472

Show key headers only | View raw


On Tue, Mar 18, 2014 at 11:32 AM, Mok-Kong Shen
<mok-kong.shen@t-online.de> wrote:
> Could someone kindly explain a phenomenon in the following where:
>
> (1) I first typed in a dictionary but got a printout in a reordered
> form.
>
> (2) I then typed in the reordered form but got a printout in the
> order that I typed in originally in (1).
>
> That is, there is no stable "standard" ordering. Why is that so?

A dictionary is simply a mapping from keys to values. It has no ordering.

> Is there a way to force a certain ordering of the printout or else
> somehow manage to get at least a certain stable ordering of the
> printout (i.e. input and output are identical)?

Yes; instead of simply printing it out (which calls repr()),
explicitly iterate over it, like this:

def display(d):
    return '{'+','.join('%r: %r'%(key,d[key]) for key in sorted(d))+'}'

>>> print(display({'label': 3, 'parent': 0, 'left child': 1, 'right child': 2}))
{'label': 3, 'left child': 1, 'parent': 0, 'right child': 2}

That will be consistent, and will also always be sorted, which will
probably be what you want for human-readable display. At least, it's
consistent as long as the keys all sort consistently, which they will
if you use simple strings. Other types of keys may not work, and in
fact mixing types may cause an exception:

>>> print(display({True:1,"Hello":2}))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in display
TypeError: unorderable types: str() < bool()

But for strings, this is the easiest way to get what you're looking for.

ChrisA

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


Thread

Ordering in the printout of a dictionary Mok-Kong Shen <mok-kong.shen@t-online.de> - 2014-03-18 01:32 +0100
  Re: Ordering in the printout of a dictionary Chris Angelico <rosuav@gmail.com> - 2014-03-18 11:48 +1100
    Re: Ordering in the printout of a dictionary John Gordon <gordon@panix.com> - 2014-03-18 02:56 +0000
    Re: Ordering in the printout of a dictionary Marc Christiansen <usenetmail@solar-empire.de> - 2014-03-18 08:36 +0100
      Re: Ordering in the printout of a dictionary Chris Angelico <rosuav@gmail.com> - 2014-03-18 18:55 +1100

csiph-web