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


Groups > comp.lang.python > #91894

Re: Please help on this sorted function

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.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 <liik.joonas@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.012
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'subject:help': 0.07; 'dict': 0.09; 'argument': 0.15; "'a',": 0.16; "'b',": 0.16; "'d',": 0.16; "'e',": 0.16; '(key,': 0.16; 'iterable:': 0.16; 'iterator': 0.16; 'lambda': 0.16; 'ordereddict': 0.16; 'pairs': 0.16; 'tells': 0.18; 'tuples': 0.22; 'to:2**1': 0.22; 'header:In- Reply-To:1': 0.24; 'sort': 0.25; 'message-id:@mail.gmail.com': 0.28; 'actual': 0.29; 'regular': 0.29; 'preserve': 0.29; 'value)': 0.29; 'function': 0.30; 'received:google.com': 0.34; 'to:addr :python-list': 0.35; 'skip:o 20': 0.35; 'turn': 0.37; 'subject:: ': 0.37; 'moment': 0.38; 'to:addr:python.org': 0.39; 'notice:': 0.67; 'special': 0.72; 'dict.': 0.84; 'to:name:python': 0.84; 'subject:this': 0.85
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:to :content-type; bh=wngyf3YLYdQ4E/DsUbVVd0d/VRKFrWqpY+lJyd95OQ8=; b=baniLL09jotFF8JDrVd4XKopbzCDKVjNY6b1loyOfNv70h0E06YzdHCjMbX5vAR67l cZsws5pgesmVL2CdwEcvah22ILL+pDLTx33mECHTDDDocLcgQsKCpI/rTXVLwkJ9KaOS Qzn5qoCDOURvXPkvUwK8WQuW5Jj7uBf4AM/IHDHkkhmLE1POmahQ6ikt8DUVOMoCnU6v YHX7GPdzOrciknbJvEl7EITwXHeWM8b6l2cNG1ggDRBPoNdg6SIE3MD9ewiqtdiKhspn LloH2PaY+2ZuLMsbVEXwyOc4bH2vsQ7VkDCajzlVYozSrogXpOQE3OwH0+PXaKBEkGTD 7uiw==
MIME-Version 1.0
X-Received by 10.107.7.84 with SMTP id 81mr35403229ioh.28.1433277751982; Tue, 02 Jun 2015 13:42:31 -0700 (PDT)
In-Reply-To <70489d38-0848-44c4-9a4c-ba2e2e9aa027@googlegroups.com>
References <37d9de72-b719-45a0-976c-441fc5898741@googlegroups.com> <70489d38-0848-44c4-9a4c-ba2e2e9aa027@googlegroups.com>
Date Tue, 2 Jun 2015 23:42:31 +0300
Subject Re: Please help on this sorted function
From Joonas Liik <liik.joonas@gmail.com>
To fl <rxjwg98@gmail.com>, Python <python-list@python.org>
Content-Type multipart/alternative; boundary=001a113ec2b445852505178efbe1
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.80.1433277754.13271.python-list@python.org> (permalink)
Lines 48
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1433277754 news.xs4all.nl 2837 [2001:888:2000:d::a6]:51163
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:91894

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

my_dict = {1: 'D', 2: 'B', 3: 'A', 4: 'E', 5: 'B'}

# dict.items() returns an iterator that returns pairs of (key, value) pairs
# the key argument to sorted tells sorted what to sort by,
operator.itemgetter is a factory function , itemgetter(1)== lambda
iterable: iterable[1]
sorted_dict = sorted(my_dict.items(), key=itemgetter(1))

# at this moment sorted dict is a generator of key-value tuples in the
right order
sorted_dict = OrderedDict(sorted_dict) # turn the generator in to an actual
dict.

# notice: regular dicts are NOT ORDERED, you need a special type of dict to
preserve the order, hence OrderedDict

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