Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Re: sort values from dictionary of dictionaries python 2.4

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


__peter__ at web

Nov 9, 2009, 6:27 AM

Post #1 of 3 (215 views)
Permalink
Re: sort values from dictionary of dictionaries python 2.4

J Wolfe wrote:

> 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

--
http://mail.python.org/mailman/listinfo/python-list


steven at REMOVE

Nov 9, 2009, 8:52 PM

Post #2 of 3 (197 views)
Permalink
Re: sort values from dictionary of dictionaries python 2.4 [In reply to]

On Mon, 09 Nov 2009 06:02:09 -0800, J Wolfe wrote:

> 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
--
http://mail.python.org/mailman/listinfo/python-list


vorticitywolfe at gmail

Nov 20, 2009, 12:07 PM

Post #3 of 3 (166 views)
Permalink
Re: sort values from dictionary of dictionaries python 2.4 [In reply to]

On Nov 9, 2:27 pm, Peter Otten <__pete...@web.de> wrote:
> J Wolfe wrote:
> > 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

Thanks Peter, That worked! :-)
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.