Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'value,': 0.04; 'python3': 0.07; 'difference,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'seemed': 0.09; 'types:': 0.09; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; '(2,': 0.16; '(3,': 0.16; '2.7.3': 0.16; 'dict': 0.16; 'igor': 0.16; 'python3.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'skip:n 70': 0.16; 'sorts': 0.16; 'typeerror:': 0.16; 'types,': 0.16; 'values:': 0.16; 'values?': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'trying': 0.19; 'feb': 0.22; '>>>': 0.22; 'example': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; '15,': 0.26; 'header:X-Complaints-To:1': 0.27; 'appreciated.': 0.29; 'chris': 0.29; 'work.': 0.31; '"",': 0.31; '>>>>': 0.31; 'explained': 0.31; 'keys': 0.31; 'work:': 0.31; 'file': 0.32; 'probably': 0.32; 'another': 0.32; '(most': 0.33; 'skip:d 20': 0.34; 'could': 0.34; 'but': 0.35; 'machine.': 0.36; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'recent': 0.39; '12,': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'even': 0.60; 'day.': 0.63; 'skip:n 10': 0.64; 'more': 0.64; '30,': 0.65; 'here': 0.66; 'frank': 0.68; 'hints': 0.68; 'results': 0.69; "'3',": 0.84; 'abc': 0.84; 'trick,': 0.84; 'from.': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Sorting dictionary by datetime value Date: Sat, 08 Feb 2014 09:27:20 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd9cc2.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 74 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391848051 news.xs4all.nl 2834 [2001:888:2000:d::a6]:56121 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65646 Frank Millman wrote: > > "Chris Angelico" wrote in message > news:CAPTjJmqDusdFC1eLbU6LF5-up__LAE-63ii0UUvAGGNem9U4+w@mail.gmail.com... >> On Sat, Feb 8, 2014 at 6:06 PM, Igor Korot wrote: >>>>>> sorted(a.items(), key=a.get) >>> [('1', datetime.datetime(2012, 12, 28, 12, 15, 30, 100)), ('3', >>> datetime.datetim >>> e(2012, 12, 28, 12, 16, 44, 100)), ('2', datetime.datetime(2012, 12, 28, >>> 12, 17, >>> 29, 100))] > > That seemed like a neat trick, so I thought I would try to understand it a > bit better in case I could use it some day. > > I am using python3. I don't know if that makes a difference, but I cannot > get it to work. > >>>> d = {1: 'abc', 2: 'xyz', 3: 'pqr'} >>>> sorted(d.items(), key=d.get) > Traceback (most recent call last): > File "", line 1, in > TypeError: unorderable types: NoneType() < NoneType() >>>> > > I know that python3 is stricter regarding ordering of non-comparable > types, but I don't see where None is coming from. > > I have python 2.7.3 on another machine. Here are the results - > >>>> d = {1: 'abc', 2: 'xyz', 3: 'pqr'} >>>> sorted(d.items(), key=d.get) > [(1, 'abc'), (2, 'xyz'), (3, 'pqr')] > > It did not crash, but it did not sort. > > Then I changed the keys to strings, to match Igor's example - > >>>> d = {'1': 'abc', '2': 'xyz', '3': 'pqr'} >>>> sorted(d.items(), key=d.get) > [('1', 'abc'), ('3', 'pqr'), ('2', 'xyz')] > > It works - now I am even more confused. > > Any hints will be appreciated. Chris has already explained it. Here you can watch the key calculation at work: >>> d = {'1': 'abc', '2': 'xyz', '3': 'pqr'} >>> def sortkey(value): ... key = d.get(value) ... print "value:", value, "sort-key:", key ... return key ... >>> sorted(d.items(), key=sortkey) value: ('1', 'abc') sort-key: None value: ('3', 'pqr') sort-key: None value: ('2', 'xyz') sort-key: None [('1', 'abc'), ('3', 'pqr'), ('2', 'xyz')] Can you change the dict to make d.get return non-None values? The OP was probably trying to mimic sorted(d, key=d.get) which sorts the dict keys by the associated dict values: >>> sorted(d, key=sortkey) value: 1 sort-key: abc value: 3 sort-key: pqr value: 2 sort-key: xyz ['1', '3', '2']