Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65645

Fwd: Sorting dictionary by datetime value

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ikorot01@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
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; '&gt;&gt;': 0.16; '11:09': 0.16; '[none,': 0.16; 'be:': 0.16; 'chris,': 0.16; 'dictionary.': 0.16; 'igor': 0.16; 'iterating': 0.16; 'sorted()': 0.16; 'sorting': 0.16; 'tuple.': 0.16; 'tuples,': 0.16; '\xa0print': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'items.': 0.19; 'feb': 0.22; '>>>': 0.22; 'email addr:gmail.com&gt;': 0.22; 'print': 0.22; '&gt;&gt;&gt;': 0.24; 'exists': 0.24; 'specify': 0.24; 'sort': 0.25; 'script': 0.25; '&gt;': 0.26; '15,': 0.26; 'this:': 0.26; '----------': 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; 'url:mailman': 0.30; 'work.': 0.31; 'code': 0.31; 'that.': 0.31; '&quot;': 0.31; 'default,': 0.31; 'keys': 0.31; 'prints': 0.31; 'probably': 0.32; 'url:python': 0.33; 'fri,': 0.33; 'device': 0.34; 'date:': 0.34; 'skip:d 20': 0.34; 'problem': 0.35; 'received:google.com': 0.35; 'url:listinfo': 0.36; 'url:org': 0.36; 'should': 0.36; 'list': 0.37; 'list.': 0.37; 'skip:& 10': 0.38; 'thank': 0.38; 'follows:': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'forwarded': 0.39; 'skip:& 20': 0.39; 'does': 0.39; 'subject:': 0.39; '12,': 0.39; 'volume': 0.39; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'hope': 0.61; 'subject:Fwd': 0.61; "you're": 0.61; 'you.': 0.62; 'back': 0.62; 'times': 0.62; 're:': 0.63; '30,': 0.65; 'series': 0.66; 'here': 0.66; 'useful.': 0.68; 'fail.': 0.84; '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=5GPJN9j+KEXV1qlDXHn5lF+oZxzg8u9ZNOJEuAYa/zc=; b=RM7mijfdlaV+F+h894gWAZ/xywRWMNiN1yTTXvDaHZjhGj9mFwiP5lJWrKFelCVB/8 kF1sLHLxglQ9H4e7/+wT39KpjFMxtmY9KQlfdmuqayQQbB2jIO05E0SLsVNA6twGmSAL w/RDgcVYD9OtVxlaKgZO68P2CQTZap9J1HfDLl65zKglSl+Haw6wrv+VGOTSCMQNjAZ5 twkxkOhHgDH4Kf4LMbag7XmRDhBpdDhA0EnrXqKqbqYq7YqpFr1aoMNmGj+sj5Pfn3bh cVK+CuZS9KUZ3c8TTr1+L17+Oh40I7zeXBfupMSMYb2puyAWkGxtw7KD/YYAxnpCi/hb reWA==
MIME-Version 1.0
X-Received by 10.58.54.15 with SMTP id f15mr14029675vep.5.1391847943346; Sat, 08 Feb 2014 00:25:43 -0800 (PST)
In-Reply-To <CA+FnnTwtrsADM2G2zRrGg7SBB5K8cEhnhaFc_n6QYomCGqUhQA@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> <CAPTjJmq9b2gs_61X2uupV-CHtknu3Pna2BUu6C3zUrpHPZw08w@mail.gmail.com> <CA+FnnTwtrsADM2G2zRrGg7SBB5K8cEhnhaFc_n6QYomCGqUhQA@mail.gmail.com>
Date Sat, 8 Feb 2014 00:25:43 -0800
Subject Fwd: Sorting dictionary by datetime value
From Igor Korot <ikorot01@gmail.com>
To python-list@python.org
Content-Type multipart/alternative; boundary=089e012942323ea1a104f1e0dabd
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.6522.1391847945.18130.python-list@python.org> (permalink)
Lines 232
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1391847945 news.xs4all.nl 2837 [2001:888:2000:d::a6]:54010
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:65645

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

---------- Forwarded message ----------
From: Igor Korot <ikorot01@gmail.com>
Date: Sat, Feb 8, 2014 at 12:25 AM
Subject: Re: Sorting dictionary by datetime value
To: Chris Angelico <rosuav@gmail.com>


Chris,


On Fri, Feb 7, 2014 at 11:58 PM, Chris Angelico <rosuav@gmail.com> wrote:

> 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
>

This code fail.
sorted_item is a list of tuples. And so iterating the list in the for loop
I will get a tuple.
It probably should be:

for key[1] in sorted_items:

Let me try that.

Thank you.


>
> Note that reverse=False is the default, so you don't need to specify that.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Fwd: Sorting dictionary by datetime value Igor Korot <ikorot01@gmail.com> - 2014-02-08 00:25 -0800

csiph-web