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: 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; '>>': 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>': 0.22; 'print': 0.22; '>>>': 0.24; 'exists': 0.24; 'specify': 0.24; 'sort': 0.25; 'script': 0.25; '>': 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; '"': 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: References: Date: Sat, 8 Feb 2014 00:25:43 -0800 Subject: Fwd: Sorting dictionary by datetime value From: Igor Korot 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --089e012942323ea1a104f1e0dabd Content-Type: text/plain; charset=ISO-8859-1 ---------- Forwarded message ---------- From: Igor Korot Date: Sat, Feb 8, 2014 at 12:25 AM Subject: Re: Sorting dictionary by datetime value To: Chris Angelico Chris, On Fri, Feb 7, 2014 at 11:58 PM, Chris Angelico wrote: > On Sat, Feb 8, 2014 at 6:53 PM, Igor Korot wrote: > > Chris, > > > > On Fri, Feb 7, 2014 at 11:09 PM, Chris Angelico > wrote: > >> 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))] > >>>>>> > >>> > >>> 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 > --089e012942323ea1a104f1e0dabd Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable


---------- Forwarded me= ssage ----------
From: Igor Korot <ikorot01@gmail.com<= /a>>
Date: Sat, Feb 8, 2014 at 12:25 AM
Subject: Re: Sorting dictionary by da= tetime value
To: Chris Angelico <
= rosuav@gmail.com>


Chris,


On Fri, Feb 7, 20= 14 at 11:58 PM, Chris Angelico <rosuav@gmail.com> wrote:
On Sat, Feb 8, 2014 at 6:53 PM, Ig= or Korot <ikorot= 01@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=3Da.get)
>>> [('1', datetime.datetime(2012, 12, 28, 12, 15, 30, 100= )), ('3', datetime.datetim
>>> e(2012, 12, 28, 12, 16, 44, 100)), ('2', datetime.date= time(2012, 12, 28, 12, 17,
>>> =A029, 100))]
>>>>>>
>>>
>>> However, trying to do the same thing from the script does not = sort the
>>> dictionary:
>>>
>>> sorted(my_dict.items(), key=3Dmy_dict.get, reverse=3DFalse) >>> for key, value in my_dict.items():
>>> =A0 =A0 =A0print 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 ite= ms:
>>
>> items =3D sorted(my_dict.items(), key=3Dmy_dict.get, reverse=3DFal= se)
>> for key, value in items:
>> =A0 =A0 print value, key
>
> Still does not work. It prints:
>
> =3D=3D=3D=3D=3D=3D=3D
> DATE TIME - EVENT
> 058f63666438&0 =A0- 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 =3D sorted(my_dict.items(), key=3Dmy_dict.get, reverse=3D= False)
> print row[19], " - Instance ID"
> print "Original values"
> for key, value in my_dict.items():
> =A0 =A0 =A0print value, key
> print "Sorted values"
> for key, value in sorted_items:
> =A0 =A0 =A0print 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= 9;t
very useful.

Try this:

sorted_items =3D sorted(my_dict.keys(), key=3Dmy_dict.get)
for key in sorted_items:
=A0 =A0 print my_dict[key], key

=
This code fail.
sorted_item is a list of tuples. And so iter= ating the list in the for loop I will get a tuple.
It probably sh= ould be:

for key[1] in sorted_items:

Le= t me try that.

Thank you.
=A0

Note that reverse=3DFalse is the default, so you don't need to specify = that.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list


--089e012942323ea1a104f1e0dabd--