I would like to sort this dictionary by the values of the inner
dictionary ‘ob’ key.
mydict =
{’WILW1′: {’fx’: ‘8.1′, ‘obtime’: ‘2009-11-07 06:45:00′, ‘ob’: ‘6.9′},
‘GRRW1′: {’fx’: ‘12.8′, ‘obtime’: ‘2009-11-07 04:15:00′, ‘ob’: ‘6.7′},
‘NASW1′: {’fx’: ‘6.8′, ‘obtime’: ‘2009-11-07 06:30:00′, ‘ob’: ‘7.1′}
}
In this case, this would become:
mysorteddic =
{’NASW1′: {’fx’: ‘6.8′, ‘obtime’: ‘2009-11-07 06:30:00′, ‘ob’: ‘7.1′},
‘WILW1′: {’fx’: ‘8.1′, ‘obtime’: ‘2009-11-07 06:45:00′, ‘ob’: ‘6.9′},
‘GRRW1′: {’fx’: ‘12.8′, ‘obtime’: ‘2009-11-07 04:15:00′, ‘ob’: ‘6.7′}
}
I have had no luck in trying to figure this out. I am restricted to
using python 2.4.3.
Any help would be appreciated!
Thanks,
Jonathan
> I would like to sort this dictionary by the values of the inner
> dictionary ‘ob’ key.
Python's built-in dictionary is unsorted by design.
> mydict =
> {’WILW1′: {’fx’: ‘8.1′, ‘obtime’: ‘2009-11-07 06:45:00′, ‘ob’: ‘6.9′},
> ‘GRRW1′: {’fx’: ‘12.8′, ‘obtime’: ‘2009-11-07 04:15:00′, ‘ob’: ‘6.7′},
> ‘NASW1′: {’fx’: ‘6.8′, ‘obtime’: ‘2009-11-07 06:30:00′, ‘ob’: ‘7.1′}
> }
>
> In this case, this would become:
>
> mysorteddic =
> {’NASW1′: {’fx’: ‘6.8′, ‘obtime’: ‘2009-11-07 06:30:00′, ‘ob’: ‘7.1′},
> ‘WILW1′: {’fx’: ‘8.1′, ‘obtime’: ‘2009-11-07 06:45:00′, ‘ob’: ‘6.9′},
> ‘GRRW1′: {’fx’: ‘12.8′, ‘obtime’: ‘2009-11-07 04:15:00′, ‘ob’: ‘6.7′}
> }
>
> I have had no luck in trying to figure this out. I am restricted to
> using python 2.4.3.
>
> Any help would be appreciated!
You may be able to work around that limitation by putting the dict items
into a list and sort that:
>>> for item in sorted(mydict.items(), key=lambda (k, v): float(v["ob"]),
reverse=True):
... print item
...
('NASW1', {'fx': '6.8', 'obtime': '2009-11-07 06:30:00', 'ob': '7.1'})
('WILW1', {'fx': '8.1', 'obtime': '2009-11-07 06:45:00', 'ob': '6.9'})
('GRRW1', {'fx': '12.8', 'obtime': '2009-11-07 04:15:00', 'ob': '6.7'})
Peter
> Hi,
>
> I would like to sort this dictionary by the values of the inner
> dictionary ‘ob’ key.
You can't sort dictionaries in Python, because they are unordered hash
tables. Giving up the ability to store items in order is one of the
things which makes them so fast.
You have five choices:
(1) Create your own implementation of a sortable dictionary, perhaps
using a red-black tree or similar. Unless you write it in C, expect it to
be massively slower than the built-in dict type. If you write it in C,
expect it to be merely slower than the built-in dict type.
(2) Write small helper functions that sort the keys from the dict when
you need them sorted. Since you (probably) only have a small number of
items in each dict, it should be fast.
(3) Use a subclass of dict that sorts the items as needed.
(4) Give up on using dictionaries for this, and use some other mapping,
like a list of (key, value) tuples. Expect it to be massively slower than
the built-in dict type.
(5) Give up on the need to have them sorted.
My advice is to go with #2, 3 or 5. Here's a basic version of 3 to get
you started:
class SortedDict(dict):
# Untested.
def items(self):
"""Return items of self in sorted order."""
L = super(SortedDict, self).items()
L.sort()
return L
You may even find a version with an appropriate licence you can freely
use if you google for "Python Sorted Dict".
--
Steven
Thanks Peter, That worked! :-)