ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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.
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.
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.
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
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.
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
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.
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.