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


Groups > comp.lang.python > #68470 > unrolled thread

Ordering in the printout of a dictionary

Started byMok-Kong Shen <mok-kong.shen@t-online.de>
First post2014-03-18 01:32 +0100
Last post2014-03-18 18:55 +1100
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  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

#68470 — Ordering in the printout of a dictionary

FromMok-Kong Shen <mok-kong.shen@t-online.de>
Date2014-03-18 01:32 +0100
SubjectOrdering in the printout of a dictionary
Message-ID<lg8462$fnk$1@news.albasani.net>
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?

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)?

Thanks in advance.

M. K. Shen
------------------------------------------

 >>> {'label': 3, 'parent': 0, 'left child': 1, 'right child': 2}
{'right child': 2, 'parent': 0, 'left child': 1, 'label': 3}
 >>> {'right child': 2, 'parent': 0, 'left child': 1, 'label': 3}
{'label': 3, 'parent': 0, 'left child': 1, 'right child': 2}

[toc] | [next] | [standalone]


#68472

FromChris Angelico <rosuav@gmail.com>
Date2014-03-18 11:48 +1100
Message-ID<mailman.8231.1395103693.18130.python-list@python.org>
In reply to#68470
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

[toc] | [prev] | [next] | [standalone]


#68476

FromJohn Gordon <gordon@panix.com>
Date2014-03-18 02:56 +0000
Message-ID<lg8ckl$aro$1@reader1.panix.com>
In reply to#68472
In <mailman.8231.1395103693.18130.python-list@python.org> Chris Angelico <rosuav@gmail.com> writes:

> > 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))+'}'

You could also use the OrderedDict type, which is subclass of dict that
preserves insertion order.

-- 
John Gordon         Imagine what it must be like for a real medical doctor to
gordon@panix.com    watch 'House', or a real serial killer to watch 'Dexter'.

[toc] | [prev] | [next] | [standalone]


#68491

FromMarc Christiansen <usenetmail@solar-empire.de>
Date2014-03-18 08:36 +0100
Message-ID<cl9lva-45d.ln1@pluto.solar-empire.de>
In reply to#68472
Chris Angelico <rosuav@gmail.com> wrote:
> On Tue, Mar 18, 2014 at 11:32 AM, Mok-Kong Shen
> <mok-kong.shen@t-online.de> wrote:
>> 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))+'}'
> 
[...]
> 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.

I would say using pprint.pprint is even easier and it works with your
failing example:

>>> pprint.pprint({True:1,"Hello":2})
{True: 1, 'Hello': 2}

Ciao
Marc

[toc] | [prev] | [next] | [standalone]


#68494

FromChris Angelico <rosuav@gmail.com>
Date2014-03-18 18:55 +1100
Message-ID<mailman.8246.1395129321.18130.python-list@python.org>
In reply to#68491
On Tue, Mar 18, 2014 at 6:36 PM, Marc Christiansen
<usenetmail@solar-empire.de> wrote:
> I would say using pprint.pprint is even easier and it works with your
> failing example:
>
>>>> pprint.pprint({True:1,"Hello":2})
> {True: 1, 'Hello': 2}
>

True. I could try to say that I prefer to offer the simpler approach
rather than encourage people to automatically reach for the nearest
hammer and hope it's right, but the fact is... I completely forgot
about pprint :)

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web