Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #65640
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4a.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.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'value,': 0.04; 'modify': 0.07; 'none,': 0.07; 'responding': 0.07; 'assuming': 0.09; 'iterate': 0.09; '11:09': 0.16; '[none,': 0.16; 'chris,': 0.16; 'dictionary.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'igor': 0.16; 'sorted()': 0.16; 'sorting': 0.16; 'tuples,': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'items.': 0.19; 'feb': 0.22; '>>>': 0.22; 'to:name :python-list@python.org': 0.22; 'print': 0.22; 'exists': 0.24; 'specify': 0.24; 'sort': 0.25; 'script': 0.25; '15,': 0.26; 'this:': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'message-id:@mail.gmail.com': 0.30; 'work.': 0.31; 'code': 0.31; 'that.': 0.31; 'default,': 0.31; 'keys': 0.31; 'prints': 0.31; 'fri,': 0.33; 'device': 0.34; 'skip:d 20': 0.34; 'problem': 0.35; 'received:google.com': 0.35; 'list.': 0.37; 'thank': 0.38; 'follows:': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'does': 0.39; '12,': 0.39; 'volume': 0.39; 'to:addr:python.org': 0.39; 'hope': 0.61; "you're": 0.61; 'you.': 0.62; 'back': 0.62; 'times': 0.62; '30,': 0.65; 'series': 0.66; 'here': 0.66; 'useful.': 0.68; 'mistake': 0.91 |
| 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=k1RhTFx9aDf1bqFybZg8hGQF4lXdK5p5+H5WvgDs02c=; b=f/7vbPyeH53zyE3CzwKPjs3sj5lZKtyUCwTmtYgbX7QW77MMU5hmNkzIMv6yd0PqCJ tpJMSXrI1lKsAzXTJr8BlrB9nWtPDkpABiDP1yfxROxnhk5ydALyuJvpQsavFbp3gCKy TBb86Ij33yzrUHqri5y5NtCE3OE7pgPKbHkZswViRjDC+51sFDNNFHmUapiVS5fqKE9/ D0Wx4Et52iCOPeItAjTZzko3YNzheljDCcr9XaGEPAS6XerbY1Daou6g6kemDEEsz/aa hjjXn9kPNP5fPnIXhfwBWfIAkaQLiPfnj9j+DNPFzAOTzC1ksaAKq4QbkmUkbqOsBorK FUCw== |
| MIME-Version | 1.0 |
| X-Received | by 10.66.164.229 with SMTP id yt5mr12983704pab.67.1391846309385; Fri, 07 Feb 2014 23:58:29 -0800 (PST) |
| In-Reply-To | <CA+FnnTz8PLn=usNyotgC6fRKVsjL6DKt9PSd4Koudwvxi3BBcw@mail.gmail.com> |
| References | <CA+FnnTxTvmSu922+Cg83jp8O_XdXXVnhQ1zaDEOd_41bONcoRQ@mail.gmail.com> <CAPTjJmqDusdFC1eLbU6LF5-up__LAE-63ii0UUvAGGNem9U4+w@mail.gmail.com> <CA+FnnTz8PLn=usNyotgC6fRKVsjL6DKt9PSd4Koudwvxi3BBcw@mail.gmail.com> |
| Date | Sat, 8 Feb 2014 18:58:29 +1100 |
| Subject | Re: Sorting dictionary by datetime value |
| From | Chris Angelico <rosuav@gmail.com> |
| To | "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.6519.1391846730.18130.python-list@python.org> (permalink) |
| Lines | 73 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1391846730 news.xs4all.nl 2965 [2001:888:2000:d::a6]:40184 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:65640 |
Show key headers only | View raw
On Sat, Feb 8, 2014 at 6:53 PM, Igor Korot <ikorot01@gmail.com> wrote:
> Chris,
>
> On Fri, Feb 7, 2014 at 11:09 PM, Chris Angelico <rosuav@gmail.com> wrote:
>> On Sat, Feb 8, 2014 at 6:06 PM, Igor Korot <ikorot01@gmail.com> 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))]
>>>>>>
>>>
>>> However, trying to do the same thing from the script does not sort the
>>> dictionary:
>>>
>>> sorted(my_dict.items(), key=my_dict.get, reverse=False)
>>> for key, value in my_dict.items():
>>> print value, key
>>>
>>> the dictionary prints with unsorted items.
>>
>> The sorted() function returns a sorted list. You're then going back to
>> the original dictionary. Instead, just iterate over the sorted items:
>>
>> items = sorted(my_dict.items(), key=my_dict.get, reverse=False)
>> for key, value in items:
>> print value, key
>
> Still does not work. It prints:
>
> =======
> DATE TIME - EVENT
> 058f63666438&0 - Instance ID
> Original values
> 2013-11-15 15:42:27.000499 User Datetime
> 2013-07-14 16:42:18.000637 Property Keys
> 2013-11-15 15:42:17.000938 Volume Device
> 2013-07-14 16:42:22.000276 Last Modify Reg Times 1
> Sorted values
> 2013-11-15 15:42:27.000499 User Datetime
> 2013-07-14 16:42:18.000637 Property Keys
> 2013-11-15 15:42:17.000938 Volume Device
> 2013-07-14 16:42:22.000276 Last Modify Reg Times 1
>
> Code is as follows:
>
> sorted_items = sorted(my_dict.items(), key=my_dict.get, reverse=False)
> print row[19], " - Instance ID"
> print "Original values"
> for key, value in my_dict.items():
> print value, key
> print "Sorted values"
> for key, value in sorted_items:
> print value, key
>
> Thank you.
Assuming you sent that privately only by mistake - hope you don't mind
me responding on-list.
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
Note that reverse=False is the default, so you don't need to specify that.
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 18:58 +1100
csiph-web