473,800 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Nested Dictionary Sorting

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.item s()', 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.


Nov 7 '06 #1
1 3404
Sam Loxton wrote:
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.item s()', 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:
>>d = {'2329513': {'ops': 20.0}, '2329492': {'ops': '80'}, '2329490':
{'ops': '50'}}
>>items = d.items()
def key(item):
.... return item[1]["ops"]
....
>>items.sort(ke y=key, reverse=True)
items
[('2329492', {'ops': '80'}), ('2329490', {'ops': '50'}), ('2329513', {'ops':
20.0})]

Peter
Nov 7 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2760
by: kbass | last post by:
I am new to Python and I am attempting to retrieve data from a database and I would like to place this data into a nested dictionary. After placing the data into a dictionary, I would like to loop through the data using for loops. How would I do this? The format that I am attempting is: Select data ... Get data ... Place data into a nested dictionary Rev = for value1 in a:
2
945
by: DelphiBlue | last post by:
I have a Nested Datagrid that is using a data relations to tie the parent child datagrids together. All is working well with the display but I am having some issues trying to sort the child datagrid. HTML Datagrid1 TemplateColumn Table Header information Detail Information
6
559
by: B0nj | last post by:
I've got a class in which I want to implement a property that operates like an indexer, for the various colors associated with the class. For instance, I want to be able to do 'set' operations like MyClass.MyColors = Color.Green or, a 'get', such as Color forecolor = MyClass.MyColors; I want to use an indexer so I can take parameters, such as the color type (e.g. "Foreground", "Background" etc.). With a single member function I couldn't...
2
2019
by: techiepundit | last post by:
I'm parsing some data of the form: OuterName1 InnerName1=5,InnerName2=7,InnerName3=34; OuterName2 InnerNameX=43,InnerNameY=67,InnerName3=21; OuterName3 .... and so on.... These are fake names I've made up to illustrate the point more clearly. (the embedded device device can't produce XML and this is what I have
3
2061
by: Jake Emerson | last post by:
I'm attempting to build a process that helps me to evaluate the performance of weather stations. The script below operates on an MS Access database, brings back some data, and then loops through to pull out statistics. One such stat is the frequency of reports from the stations ('char_freq'). I have a collection of methods that operate on the data to return the 'char_freq' and this works great. However, when the process goes to insert the...
12
1465
by: Rich Shepard | last post by:
I want to code what would be nested "for" loops in C, but I don't know the most elegant way of doing the same thing in python. So I need to learn how from you folks. Here's what I need to do: build a database table of 180 rows. Each row contains 31 columns: the first is an automatically incremented integer as the primary key; the next two fields can each contain one of three strings held in dictionaries, the last 28 fields are random...
1
2135
by: hermesbaby | last post by:
I have a CSV-File which I convert to an array like this: , , , , , ]
1
3363
by: qbob | last post by:
I have data that is essentially tree-like, and to store it/iterate over it I have been using nested dictionaries, eg: Dictionary<int, Dictionary<string, Dictionary<double>>> data = new ... to initialize the data structure, I load in the data - imagine it's a List of DataItem objects with fields (int id1, string id2, double value). then I do: foreach(DataItem item in items) { if (!data.ContainsKey(item.id1)) data = new...
2
6289
by: =?Utf-8?B?RGF2aWQgTW9ycmlz?= | last post by:
I am trying to create a nested Dictionary and get an error that seems odd to me. Here is my declaration: private IDictionary<Guid, IDictionary<Guid, string>> myNestedDictionary = new Dictionary<Guid, Dictionary<Guid, string>>(); I get an error unless I nest a Dictionary and not an IDictionary: Error 3 Cannot implicitly convert type
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10275
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7576
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.