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


Groups > comp.lang.python > #91914

Re: Please help on this sorted function

References <37d9de72-b719-45a0-976c-441fc5898741@googlegroups.com> <70489d38-0848-44c4-9a4c-ba2e2e9aa027@googlegroups.com>
Date 2015-06-03 10:01 +1000
Subject Re: Please help on this sorted function
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.86.1433289704.13271.python-list@python.org> (permalink)

Show all headers | View raw


On Wed, Jun 3, 2015 at 6:25 AM, fl <rxjwg98@gmail.com> wrote:
> I am still new to Python. How to get the sorted dictionary output:
>
> {1: 'D', 2: 'B', 3: 'A', 4: 'E', 5: 'B'}

Since dictionaries don't actually have any sort of order to them, the
best thing to do is usually to simply display it in order. And there's
a very handy function for doing that: a pretty-printer.

>>> import pprint
>>> pprint.pprint({1: 'D', 2: 'B', 5: 'B', 4: 'E', 3: 'A'})
{1: 'D', 2: 'B', 3: 'A', 4: 'E', 5: 'B'}

This one comes with Python, so you can use it as easily as that above
example. Or you could do it this way:

>>> from pprint import pprint
>>> pprint({1: 'D', 2: 'B', 5: 'B', 4: 'E', 3: 'A'})
{1: 'D', 2: 'B', 3: 'A', 4: 'E', 5: 'B'}

For a lot of Python data structures, this will give you a tidy and
human-readable display.

ChrisA

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


Thread

Please help on this sorted function fl <rxjwg98@gmail.com> - 2015-06-02 13:20 -0700
  Re: Please help on this sorted function fl <rxjwg98@gmail.com> - 2015-06-02 13:25 -0700
    Re: Please help on this sorted function Joonas Liik <liik.joonas@gmail.com> - 2015-06-02 23:42 +0300
      Re: Please help on this sorted function Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-06-03 18:21 +1000
    Re: Please help on this sorted function Chris Angelico <rosuav@gmail.com> - 2015-06-03 10:01 +1000
  Re: Please help on this sorted function Joonas Liik <liik.joonas@gmail.com> - 2015-06-02 23:32 +0300
  Re: Please help on this sorted function Gary Herron <gherron@digipen.edu> - 2015-06-02 14:00 -0700

csiph-web