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
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()
>
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())
"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,
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
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()?
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
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
-- 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:
> &
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',
>>
"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
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:
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
"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,
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
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
16 matches
Mail list logo