Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'none,': 0.07; '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; 'replied': 0.09; 'seemed': 0.09; 'types:': 0.09; 'python': 0.11; 'wrote': 0.14; '(2,': 0.16; '(3,': 0.16; '2.7.3': 0.16; '[none,': 0.16; 'igor': 0.16; 'python3.': 0.16; 'realising': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:n 70': 0.16; 'sorting': 0.16; 'tuples,': 0.16; 'typeerror:': 0.16; 'types,': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'feb': 0.22; '>>>': 0.22; 'example': 0.22; 'print': 0.22; 'exists': 0.24; '15,': 0.26; 'this:': 0.26; 'post': 0.26; 'header:X-Complaints-To:1': 0.27; 'chris': 0.29; 'work.': 0.31; '"",': 0.31; '>>>>': 0.31; 'keys': 0.31; 'file': 0.32; 'another': 0.32; '(most': 0.33; 'received:co.za': 0.34; 'received:za': 0.34; 'skip:d 20': 0.34; 'could': 0.34; 'problem': 0.35; 'but': 0.35; 'machine.': 0.36; 'much.': 0.36; 'thanks': 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; "you're": 0.61; 'day.': 0.63; 'skip:n 10': 0.64; 'more': 0.64; '30,': 0.65; 'series': 0.66; 'here': 0.66; 'frank': 0.68; 'useful.': 0.68; 'results': 0.69; 'received:41': 0.70; 'trick,': 0.84; 'from.': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Frank Millman" Subject: Re: Sorting dictionary by datetime value Date: Sat, 8 Feb 2014 10:22:05 +0200 References: X-Gmane-NNTP-Posting-Host: 41-135-99-195.dsl.mweb.co.za X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.3790.4657 X-RFC2646: Format=Flowed; Response X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.4913 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: 67 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391847737 news.xs4all.nl 2891 [2001:888:2000:d::a6]:50522 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65643 "Frank Millman" wrote in message news:ld4ocf$9rg$1@ger.gmane.org... > > "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. > As Chris replied in another post - > The problem here is actually your key function. my_dict.items() > returns a series of two-item tuples, none of which exists in your > dictionary; so you're actually sorting [None, None, None], which isn't > very useful. > > Try this: > > sorted_items = sorted(my_dict.keys(), key=my_dict.get) > for key in sorted_items: > print my_dict[key], key > Without realising it, you have answered all my questions - thanks very much. Frank