Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #65642
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| 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; 'operator': 0.03; 'subsequent': 0.05; '(so': 0.07; 'element': 0.07; 'none,': 0.07; '[1,': 0.09; 'difference,': 0.09; 'false.': 0.09; 'iterate': 0.09; 'types:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '(2,': 0.16; '(3,': 0.16; '[none,': 0.16; 'callable': 0.16; 'fetch': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'itemgetter': 0.16; 'python3.': 0.16; 'tuple,': 0.16; 'typeerror:': 0.16; 'sat,': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'trying': 0.19; 'feb': 0.22; '>>>': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'sort': 0.25; 'second': 0.26; 'post': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'message- id:@mail.gmail.com': 0.30; 'work.': 0.31; '"",': 0.31; '>>>>': 0.31; 'keys': 0.31; 'values.': 0.31; 'file': 0.32; 'probably': 0.32; '(most': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'object,': 0.36; 'error.': 0.37; 'list': 0.37; 'skip:o 20': 0.38; 'pm,': 0.38; 'that,': 0.38; 'recent': 0.39; 'explain': 0.39; 'though,': 0.39; 'either': 0.39; 'simply': 0.61; "you're": 0.61; 'more': 0.64; 'effectively': 0.66; 'frank': 0.68; 'useful.': 0.68; 'to:none': 0.92 |
| 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:cc :content-type; bh=fJiy6uDFZyX4FVXfAvenRkkcavwUTNF1JcW910bH5zs=; b=EfdmdKwZhDTwpQiunaeMDcreuYunVTMP+dMdrLDTp56AFMlR0VG7xhNDnCzMIu1EMv XavrNMTGdR5rlCuyFyjpdPYslciWP1p+ZEcMvvnlGyM/KAcuqtMVYBnajfGvP3ZoZpQc C9cY1t0Bz4IwNpqiczcUFjg88B8hrUmV81F+3ZsOPJL6c3tdcMxaedMzmDx8yKaZxgjr vGXjaZLSJFr2IvnHSRouFc2yqBso9MNdz6bB7HquoBJ+CU8pUAB7aAHzV2V4Otcdsgz6 vTNLcFrOr4nEWo4lptRRiH9cIjzoM/bUT10To24MH8y3T4bBD6U9d283TRsZOXx6yC04 X8rA== |
| MIME-Version | 1.0 |
| X-Received | by 10.68.182.165 with SMTP id ef5mr14853pbc.169.1391847488209; Sat, 08 Feb 2014 00:18:08 -0800 (PST) |
| In-Reply-To | <ld4ocf$9rg$1@ger.gmane.org> |
| References | <CA+FnnTxTvmSu922+Cg83jp8O_XdXXVnhQ1zaDEOd_41bONcoRQ@mail.gmail.com> <CAPTjJmqDusdFC1eLbU6LF5-up__LAE-63ii0UUvAGGNem9U4+w@mail.gmail.com> <ld4ocf$9rg$1@ger.gmane.org> |
| Date | Sat, 8 Feb 2014 19:18:08 +1100 |
| Subject | Re: Sorting dictionary by datetime value |
| From | Chris Angelico <rosuav@gmail.com> |
| Cc | "python-list@python.org" <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| 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.6520.1391847492.18130.python-list@python.org> (permalink) |
| Lines | 40 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1391847492 news.xs4all.nl 2856 [2001:888:2000:d::a6]:46158 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:65642 |
Show key headers only | View raw
On Sat, Feb 8, 2014 at 7:03 PM, Frank Millman <frank@chagford.com> wrote:
> 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 "<stdin>", line 1, in <module>
> TypeError: unorderable types: NoneType() < NoneType()
>>>>
You probably hadn't seen my subsequent post yet, in which I explain
what's going on here.
In Python 2, "None > None" is simply False. (So is "None < None",
incidentally.) Py3 makes that an error. But in all your examples,
you're effectively trying to sort the list [None, None, None], which
is never going to be useful. What you can do, though, is either sort
items using itemgetter to sort by the second element of the tuple, or
sort keys using dict.get.
Using 3.4.0b2:
>>> d = {1: 'abc', 2: 'xyz', 3: 'pqr'}
>>> sorted(d.keys(), key=d.get)
[1, 3, 2]
You don't get the values that way, but you get the keys in their
correct order, so you can iterate over that and fetch the
corresponding values. Alternatively, this is a bit more verbose, but
works on items():
>>> import operator
>>> sorted(d.items(), key=operator.itemgetter(1))
[(1, 'abc'), (3, 'pqr'), (2, 'xyz')]
operator.itemgetter(1) returns a callable that, when passed some
object, returns that_object[1].
ChrisA
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Sorting dictionary by datetime value Chris Angelico <rosuav@gmail.com> - 2014-02-08 19:18 +1100
csiph-web