2

I have a python dict which contains unsorted key/value pairs, where the key is a datetime and the value is an int.

{"1.12.2015":5, "4.12.2015":3, "2.12.2015":6, ...}

I now wish to read the keys, and their values sorted by date in order to plot them using pyplot.

The result should be like:

first pair:
1.12.2015 : 5
second: 
2.12.2015 : 6
third:
4.12.2015 : 3

the question is, what is the best way to get the dates and values out in the right order. It would be best if there was an option which runs in python 2.6

mat
  • 77
  • 1
  • 1
  • 6
  • Possible duplicate of [Sorting dictionary keys in python](http://stackoverflow.com/questions/575819/sorting-dictionary-keys-in-python) – Dylan Lawrence Dec 02 '15 at 17:37
  • @Dylan Lawrence this shows sorting by values, but i wish to sort by key, or can this be achived the same way? – mat Dec 02 '15 at 17:44
  • Please see answer: http://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value – Dylan Lawrence Dec 02 '15 at 17:45

3 Answers3

4

Use a lambda as the key to sorted casting each key to a datetime object:

 from datetime import datetime

d = {"1.12.2015": 5, "4.12.2015": 3, "2.12.2015": 6}

for k in sorted(d, key=lambda x: datetime.strptime(x, "%d.%m.%Y")):
    print(k, d[k])

Output:

1.12.2015 5
2.12.2015 6
4.12.2015 3

As per you comment if you actually have datetime objects already then you just simply sort:

    for k in sorted(d):
        print(k, d[k])
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
4

you can do this,

dic={"1.12.2015":5, "4.12.2015":3, "2.12.2015":6}
k = sorted(dic, key=lambda d: map(int, d.split('.')))
for i in k:
    print i," : ",dic[i]

cheers!

sameera sy
  • 1,708
  • 13
  • 19
0

Is this more or less what you are looking for?

data = {"1.12.2015":5, "4.12.2015":3, "2.12.2015":6}

from datetime import datetime

for k,v in sorted([(datetime.strptime(k, '%d.%m.%Y'), v) for k,v in data.iteritems()]):
    print datetime.strftime(k, '%d.%m.%Y'), v
KT.
  • 10,815
  • 4
  • 47
  • 71
  • this is what i was looking for, ty – mat Dec 02 '15 at 18:05
  • 1
    this creates a list for no reason, casts to datetime objects and then calls datetime.strftime to create a string again, I really don't follow the logic – Padraic Cunningham Dec 02 '15 at 18:07
  • @Padraic Cunningham the for-loop + sorted method is what i was looking for. Since the dates are stored as datetime objects i don't need to cast them like in this example which makes the actual implementation a lot cleaner. – mat Dec 02 '15 at 18:22
  • 1
    @mat, then you just need `sorted(d)` or `sorted(d.items())`, there is no reason to first create a list or call datetime.strftime, if you were doing anything you would call strftime on the datetime object or call one of the various methods to get a string representation – Padraic Cunningham Dec 02 '15 at 18:25
  • @PadraicCunningham: You are right. However, note that in older Pythons `d.items()` would create an intermediate list. Another intermediate list is created by `sorted()` under covers. Hence, there's a whole bunch of intermediate lists being created behind the scenes, and it is not obvious that the list comprehension version is somehow less efficient. The particular syntax (list comprehension around an iteritems) was chosen because this seemed as the most "flexible" form for an example to build upon. Your version is certainly more concise. – KT. Dec 02 '15 at 19:26
  • 1
    You could still `sorted((datetime.strptime(k, '%d.%m.%Y'), v) for k,v in data.iteritems())` at worst, I was more referring to converting to datetime objects and then calling `datetime.strftime` to convert back to strings. If you wanted to use strftime you would usually call it on the object but `str(dt_object)` would also do the same – Padraic Cunningham Dec 02 '15 at 20:11
  • I happen to use `strftime` so rarely (i.e. never) I did not come to think the datetime object has it as a method :) – KT. Dec 02 '15 at 20:31