Re: Sorting dictionary by datetime value

2014-02-10 Thread Asaf Las
Why not use collections.OrderedDict ? There are nice examples in doc: http://docs.python.org/3.3/library/collections.html?highlight=ordered#ordereddict-examples-and-recipes -- https://mail.python.org/mailman/listinfo/python-list

Re: Sorting dictionary by datetime value

2014-02-10 Thread Chris Angelico
On Tue, Feb 11, 2014 at 1:31 PM, pete suchsland wrote: > How about make it simple by using sorted(a.values()) ... > a = {} a['first'] = datetime.datetime.now() a['second'] = datetime.datetime.now() a['third'] = datetime.datetime.now() a['forth'] = datetime.datetime.now() >

Re: Sorting dictionary by datetime value

2014-02-10 Thread pete suchsland
How about make it simple by using sorted(a.values()) ... >>> a = {} >>> a['first'] = datetime.datetime.now() >>> a['second'] = datetime.datetime.now() >>> a['third'] = datetime.datetime.now() >>> a['forth'] = datetime.datetime.now() >>> a['fifth'] = datetime.datetime.now() >>> sorted(a.values())

Re: Sorting dictionary by datetime value

2014-02-08 Thread Frank Millman
"Peter Otten" <__pete...@web.de> wrote in message news:ld4pon$ni9$1...@ger.gmane.org... > Frank Millman wrote: > > Here you can watch the key calculation at work: > d = {'1': 'abc', '2': 'xyz', '3': 'pqr'} def sortkey(value): > ... key = d.get(value) > ... print "value:", value,

Re: Sorting dictionary by datetime value

2014-02-08 Thread Larry Hudson
On 02/07/2014 11:06 PM, Igor Korot wrote: Hi, ALL, I'm trying to do a very easy task: sort python dictionary by value where value is a datetime object. When trying to do that in Python shell everthing works as expected. C:\Documents and Settings\Igor.FORDANWORK>python Python 2.7.5 (default, May

Re: Sorting dictionary by datetime value

2014-02-08 Thread Tim Chase
On 2014-02-08 19:29, Chris Angelico wrote: > On Sat, Feb 8, 2014 at 7:25 PM, Igor Korot > wrote: > >> Try this: > >> > >> sorted_items = sorted(my_dict.keys(), key=my_dict.get) > > > > This code fail. > > Actually, it's a list of keys - notice that I changed > my_dict.items() into my_dict.keys()?

Re: Sorting dictionary by datetime value

2014-02-08 Thread Igor Korot
Thank you. That worked. And no, I didn't notice that change. :( On Sat, Feb 8, 2014 at 12:29 AM, Chris Angelico wrote: > On Sat, Feb 8, 2014 at 7:25 PM, Igor Korot wrote: > >> Try this: > >> > >> sorted_items = sorted(my_dict.keys(), key=my_dict.get) > >> for key in sorted_items: > >> prin

Re: Sorting dictionary by datetime value

2014-02-08 Thread Chris Angelico
On Sat, Feb 8, 2014 at 7:25 PM, Igor Korot wrote: >> 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 t

Fwd: Sorting dictionary by datetime value

2014-02-08 Thread Igor Korot
-- 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: > &

Re: Sorting dictionary by datetime value

2014-02-08 Thread Peter Otten
Frank Millman wrote: > > "Chris Angelico" wrote in message > news:captjjmqdusdfc1elbu6lf5-up__lae-63ii0uuvaggnem9u...@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', >>

Re: Sorting dictionary by datetime value

2014-02-08 Thread Frank Millman
"Frank Millman" wrote in message news:ld4ocf$9rg$1...@ger.gmane.org... > > "Chris Angelico" wrote in message > news:captjjmqdusdfc1elbu6lf5-up__lae-63ii0uuvaggnem9u...@mail.gmail.com... >> On Sat, Feb 8, 2014 at 6:06 PM, Igor Korot wrote: >> sorted(a.items(), key=a.get) >>> [('1', datetim

Re: Sorting dictionary by datetime value

2014-02-08 Thread Chris Angelico
On Sat, Feb 8, 2014 at 7:03 PM, Frank Millman 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 "", line 1, in > TypeError:

Re: Sorting dictionary by datetime value

2014-02-08 Thread Chris Angelico
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

Re: Sorting dictionary by datetime value

2014-02-08 Thread Frank Millman
"Chris Angelico" wrote in message news:captjjmqdusdfc1elbu6lf5-up__lae-63ii0uuvaggnem9u...@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,

Re: Sorting dictionary by datetime value

2014-02-07 Thread Chris Angelico
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

Sorting dictionary by datetime value

2014-02-07 Thread Igor Korot
Hi, ALL, I'm trying to do a very easy task: sort python dictionary by value where value is a datetime object. When trying to do that in Python shell everthing works as expected. C:\Documents and Settings\Igor.FORDANWORK>python Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Inte