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: 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 To: fl , Python 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --001a113ec2b445852505178efbe1 Content-Type: text/plain; charset=UTF-8 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 --001a113ec2b445852505178efbe1 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
my_dict =3D=C2=A0{1: 'D', 2: 'B', 3: 'A', 4: 'E', 5: = 9;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 ,=C2=A0itemgetter(1)=3D=3D lambd= a iterable: iterable[1]=
sorted_dict = =3D sorted(my_dict.items(), key=3Ditemgetter(1))

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

# notice: regular dicts are N= OT ORDERED, you need a special type of dict to preserve the order, hence Or= deredDict

=

--001a113ec2b445852505178efbe1--