Nested Dictionary Sorting

S

Sam Loxton

Hi,

I am fairly new to the python language and am trying to sort a nested
Dictionary of a Dictionary which I wish to sort by value. The dictionary
does not have to be restructured as I only need it sorted in this way
for printing purposes.

The following is an example of my Dictionary printed with 'print
dictionary.items()', where '2329513' is the key of the first hash, 'ops'
is the key of the second hash and '50.0' is the value of the second hash
which I would like to sort by:
[('2329513', {'ops': 20.0}), ('2329492', {'ops': '80'}), ('2329490',
{'ops': '50'})]

I hope to sort these first keys by the value of the 'ops' key from
highest to lowest value to give the following result:
[('2329492', {'ops': '80'}), ('2329490', {'ops': '50'}), ('2329513',
{'ops': 20.0})]

Thanks in advance for any help,
Sam.
 
D

Dennis Lee Bieber

The following is an example of my Dictionary printed with 'print
dictionary.items()', where '2329513' is the key of the first hash, 'ops'
is the key of the second hash and '50.0' is the value of the second hash
which I would like to sort by:
[('2329513', {'ops': 20.0}), ('2329492', {'ops': '80'}), ('2329490',
{'ops': '50'})]
Well, firstly, that is a LIST of TUPLES consisting of pairs of a
STRING and a one-element DICTIONARY...

The only "Key" in that creature is "ops", and it is repeated in each
tuple's dictionary element, making it essentially waste.

Also note that two of these dictionarys have STRING values, and the
third has a floating point number.

The same information is carried by:

{ 2329513 : 20.0, 2329492 : "80", 2329490 : "50" }

I hope to sort these first keys by the value of the 'ops' key from
highest to lowest value to give the following result:
[('2329492', {'ops': '80'}), ('2329490', {'ops': '50'}), ('2329513',
{'ops': 20.0})]
d = { 2329513 : 20.0, 2329492 : "80", 2329490 : "50" }
d {2329513: 20.0, 2329490: '50', 2329492: '80'}
lt = [ (v, k) for (k, v) in d.items() ]
lt [(20.0, 2329513), ('50', 2329490), ('80', 2329492)]
ol = [ (k, v) for (v, k) in reversed(sorted(lt)) ]
ol
[(2329492, '80'), (2329490, '50'), (2329513, 20.0)]
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
P

Peter Otten

Sam said:
I am fairly new to the python language and am trying to sort a nested
Dictionary of a Dictionary which I wish to sort by value. The dictionary
does not have to be restructured as I only need it sorted in this way
for printing purposes.

The following is an example of my Dictionary printed with 'print
dictionary.items()', where '2329513' is the key of the first hash, 'ops'
is the key of the second hash and '50.0' is the value of the second hash
which I would like to sort by:
[('2329513', {'ops': 20.0}), ('2329492', {'ops': '80'}), ('2329490',
{'ops': '50'})]

I hope to sort these first keys by the value of the 'ops' key from
highest to lowest value to give the following result:
[('2329492', {'ops': '80'}), ('2329490', {'ops': '50'}), ('2329513',
{'ops': 20.0})]

If Dennis' remarks don't apply because you simplified your problem for the
post:
.... return item[1]["ops"]
....[('2329492', {'ops': '80'}), ('2329490', {'ops': '50'}), ('2329513', {'ops':
20.0})]

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top