LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-18-2016, 08:49 AM   #1
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Ordering Python structure by dict key


I'm hoping that Python counts as a proper programming language here.

I'm currently writing a Python program to analyse ext3/4 filesystems and part of my program builds a Python structure called dirent while traversing a directory's data blocks. Sample dirent contents are as follows:

Code:
{223: {'rec_len': 44,
       'name_len': 36,
       'file_type': 1,
       'name': 'c8d653e2b6be79114668c7b9d3c8f37f.png'},
 428: {'rec_len': 4052,
       'name_len': 36,
       'file_type': 1,
       'name': '8d6eaaa5e649889966028dc059831f4e.png'},
 120: {'rec_len': 44,
       'name_len': 36,
       'file_type': 1,
       'name': 'd75e4930a5f82d09111eb1a12c214f7d.png'},
 1193: {'rec_len': 44,
        'name_len': 36,
        'file_type': 1,
        'name': 'c6c41617f1c1bc669fd897dd527136e9.png'},
 278: {'rec_len': 3980,
       'name_len': 36,
       'file_type': 1,
       'name': '45ff55acd424e43983714352b70ae1fa.png'},
 1318: {'rec_len': 44,
        'name_len': 36,
        'file_type': 1,
        'name': 'd57958bbb31272679376b68367aebc7d.png'},
 279: {'rec_len': 4024,
       'name_len': 36,
       'file_type': 1,
       'name': '3530722f4852d3ff971d13770cbd7742.png'},
 1203: {'rec_len': 72,
        'name_len': 36,
        'file_type': 1,
        'name': 'cbe30ff8539c2fc5bff6cd27dd238b8c.png'},
 200: {'rec_len': 4024,
       'name_len': 36,
       'file_type': 1,
       'name': '9590eff28f0a8f57e7997f4032f028ab.png'}}
The first number in each record is the inode number.

What I would like to do is order this structure so that it is ordered by the 'name' field. However I'm having trouble getting my head around how to do this.

Essentially I have a dict with one field which is itself a dict with four fields, and I'd like to order the whole dict using one of the subsidiary dict values as a key.

Is this possible?
 
Old 03-18-2016, 09:30 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Depends what you're trying to do.

You can get the list of values with the dict's .values() method and sort that list by name.

There's a collections.OrderedDict class that's sorted by insertion order.

If some combination of the above isn't adequate, then you need to rethink how you're structuring the data.

Last edited by dugan; 03-18-2016 at 09:45 AM.
 
1 members found this post helpful.
Old 03-18-2016, 09:40 AM   #3
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048

Original Poster
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by dugan View Post
There's a collections.OrderedDict class that's sorted by insertion order.
Thanks dugan. I've used that class before, when I've needed to remember insertion order, but in this case the insertion order is of no consequence.

Edit: Sorry, I understand now. The resulting ordered dict structure can be stored in an OrderedDict structure in order to retain the ordering. I'll do that, but my problem is working out if the original dict can easily be sorted with the sorted() function in the way I'm looking for, without having to manually traverse the data structure, breaking the dict up into its components and doing the sorting on these.

Last edited by hydrurga; 03-18-2016 at 09:48 AM.
 
Old 03-18-2016, 09:46 AM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
See my edit.
 
Old 03-18-2016, 09:55 AM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
You can run sorted() on the keys or the values, and doing so gives you a list of either the keys or the values. You can't reorder the dict using a single call to sorted(), especially since dict ordering is technically undefined.

Last edited by dugan; 03-18-2016 at 11:50 AM.
 
Old 03-18-2016, 10:00 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I do not have much Python experience, so I played with the idea in Ruby.

My process was to create another hash (dict) with the name as the key and your original keys (eg. 223). The I simply sorted that hash and used the sorted order to display the original data in the form needed.

Not sure if that helps at all ... probably best to listen to dugan
 
1 members found this post helpful.
Old 03-18-2016, 10:53 AM   #7
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048

Original Poster
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Thanks to both of you. Interesting approach, grail.

Just in case my intentions weren't clear, I was hoping that a one command sort would create (with the target an OrderedDict):

Code:
{279: {'rec_len': 4024,
       'name_len': 36,
       'file_type': 1,
       'name': '3530722f4852d3ff971d13770cbd7742.png'},

 278: {'rec_len': 3980,
       'name_len': 36,
       'file_type': 1,
       'name': '45ff55acd424e43983714352b70ae1fa.png'},

 428: {'rec_len': 4052,
       'name_len': 36,
       'file_type': 1,
       'name': '8d6eaaa5e649889966028dc059831f4e.png'},

 ...
i.e. the exact same structure as the original, but ordered by ascending 'name'.

You're right though, dugan. It was far easier for me, without knowing how to achieve the above programatically, to change my original structure to base the master key on the name rather than the inode.

So I did exactly that, creating the structure, and then following up with a:

Code:
dirent = OrderedDict(sorted(dirent.items())
There's still this niggling thought that my original aim should be achievable, that I should somehow be able to order the whole structure on the values of sub-dict fields, but if it doesn't exist, it doesn't exist.

Marked as "Solved" with my thanks again.
 
Old 03-18-2016, 12:12 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well if it helps, here is the ruby I whipped up for the same:
Code:
#!/usr/bin/env ruby

sorted_h = {}

h = {223 => {rec_len: 44,
       name_len: 36,
       file_type: 1,
       name: 'c8d653e2b6be79114668c7b9d3c8f37f.png'},
 428 => {rec_len: 4052,
       name_len: 36,
       file_type: 1,
       name: '8d6eaaa5e649889966028dc059831f4e.png'},
 120 => {rec_len: 44,
       name_len: 36,
       file_type: 1,
       name: 'd75e4930a5f82d09111eb1a12c214f7d.png'},
 1193 => {rec_len: 44,
        name_len: 36,
        file_type: 1,
        name: 'c6c41617f1c1bc669fd897dd527136e9.png'},
 278 => {rec_len: 3980,
       name_len: 36, 
       file_type: 1,
       name: '45ff55acd424e43983714352b70ae1fa.png'},
 1318 => {rec_len: 44, 
        name_len: 36, 
        file_type: 1,
        name: 'd57958bbb31272679376b68367aebc7d.png'},
 279 => {rec_len: 4024,
       name_len: 36, 
       file_type: 1,
       name: '3530722f4852d3ff971d13770cbd7742.png'},
 1203 => {rec_len: 72,
        name_len: 36,
        file_type: 1,
        name: 'cbe30ff8539c2fc5bff6cd27dd238b8c.png'},
 200 => {rec_len: 4024,
       name_len: 36,
       file_type: 1,
       name: '9590eff28f0a8f57e7997f4032f028ab.png'}
}

names = {}

h.each{|k,v| names[v[:name]] = k}

names.keys.sort.each do |key|
  sorted_h[names[key]] = h[names[key]]
end

puts sorted_h
 
1 members found this post helpful.
Old 03-18-2016, 12:31 PM   #9
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048

Original Poster
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Maybe I should be changing languages.
 
Old 03-18-2016, 01:13 PM   #10
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
The Python equivalent of the above would be:

Code:
import collections

h = {} # You know what data goes in here. I'll leave it out for this example.

sorted_h = collections.OrderedDict()

for key, value in sorted(h.iteritems(), key=lambda item: item[1]['name']):
    sorted_h[key] = value

# sorted_h is sorted by name
Quote:
Originally Posted by hydrurga View Post
Maybe I should be changing languages.
Well, I can see that grail's code uses a standard dictionary for both the original and the sorted dictionary. In Python you absolutely need an OrderedDict for the sorted one, because the order of keys and values in standard dictionaries is undefined. Whether that real technical difference is a reason to change languages is up to you.

Last edited by dugan; 03-18-2016 at 02:14 PM.
 
1 members found this post helpful.
Old 03-18-2016, 01:33 PM   #11
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048

Original Poster
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Thanks dugan, that is indeed what the doctor ordered (any puns intentional).

I'm too used to Python at the moment anyway. There are just some things I seem to have conceptual difficulties with, and Python dicts and lists are two of them (I know that they should be simple, I use them, but I just haven't got to grips with them yet as regards having a real understanding).
 
Old 03-18-2016, 01:44 PM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Mine also has a cheat that is not obvious ... names.keys, this returns an array of the keys which is then sorted.

I did do one with some of the steps combined as well, but it is confusing if you are not familiar with ruby.

As a side note, I did initially start learning python (3+), but was lead to Ruby by kurumi here on LQ
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] python: return dict/sturcture. cant access attributes. PoleStar Linux - Newbie 2 12-11-2015 04:34 PM
Python dict query and assign values problems ArthurHuang Programming 2 03-13-2012 04:18 AM
Ices0 and python: trying to set up server/client structure hoes Linux - Software 0 10-08-2009 01:54 PM
Python/PyQt4 - Newbie Structure question ocularb0b Programming 0 01-28-2008 09:42 AM
python exception like structure with a timer shanenin Programming 4 08-20-2005 10:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration