Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show(dict) now sorted by key #16743

Closed
wants to merge 1 commit into from
Closed

Conversation

bjarthur
Copy link
Contributor

@bjarthur bjarthur commented Jun 3, 2016

sorting vastly improves human readability.

left intact is the unsorted compact output. someone might need to know the true order of the pairs in the dict. it is also not trivial to sort this due to the need to handle circularly referential dicts.

@quinnj
Copy link
Member

quinnj commented Jun 3, 2016

-1, I think the danger here is that a user sees the output of a Dict and assumes a certain order, then iterates over the key-value pairs expecting that same order. I don't think we should do this unless we truly move to an ordered dict representation; ref #10116

@JeffBezanson
Copy link
Sponsor Member

Furthermore, with ordered dicts the order is significant, but not necessarily the same as the sorted order.

@kmsquire
Copy link
Member

kmsquire commented Jun 3, 2016

@bjarthur, just in case you aren't aware, DataStructures.jl contains an actual SortedDict implementation (as well as a few others).

@bjarthur
Copy link
Contributor Author

bjarthur commented Jun 3, 2016

thanks for the reminder about SortedDict. i suppose that's the most parsimonious solution to my needs.

does seem a bit cumbersome though as i never care about the unsorted order. my concern is just about quickly finding by eye the key/value i'm looking for. for example:

SortedDict(Pkg.installed())
SortedDict(matread("foo.mat"))

perhaps i should ask beforehand whether i should tackle REPL completions on Dict keys. i want to be able to my_dict["key_prefix\tab, much like one can do for variables, modules, etc. is this on anyone's short list?

@bjarthur bjarthur closed this Jun 3, 2016
@rfourquet
Copy link
Member

+1.
I understand that it can be confusing for some beginners, but I agree with @bjarthur that it vastly improves readability.
Most of the time, I wouldn't use a DataStructures.SortedDict because it's not needed by the program, but that doesn't mean I wouldn't greatly enjoy a sorted representation in the terminal.
For reference, the pprint function in python (which is I think enabled by default in ipython) does sort dictionaries ouptut.

@StefanKarpinski
Copy link
Sponsor Member

This has performance implications for printing very large Dicts. I don't think it's a good idea. I do, however, think that making Dict ordered is a good idea that we should go ahead with.

@bjarthur
Copy link
Contributor Author

bjarthur commented Jun 9, 2016

@Keno are there any plans to implement tab completion on Dict keys? is this a feature that would be favorably viewed? i'm willing to take a stab at it if it's not already on the horizon.

@rfourquet
Copy link
Member

There are indeed performance implications, but without sorting, showing the dict amounts to showing randomly (more or less) selected element, which is not ideal either. Anyway, if Dict becomes ordered in the future, the status-quo is fine for now.

@oscardssmith
Copy link
Member

@StefanKarpinski I think ordered dict by default is a really bad idea. ordered dicts have their place, but there are a large number of places where the order makes no difference for the algorithm, and making dict ordered by default would slow down these programs significantly (up to ~2x if dict read/writes are where most time is spent). I do think that there should be an ability show dicts in order, but I think that this should be a choice.

@eschnett
Copy link
Contributor

I assume there are also cases where the keys do not even have a well-defined order...

@oscardssmith
Copy link
Member

specifically strings, vectors etc.

@StefanKarpinski
Copy link
Sponsor Member

StefanKarpinski commented Aug 18, 2016

I'm talking about ordered dicts, not sorted dicts. An ordered dict maintains the ordering in which keys are inserted; in particular, it does not require the keys to be comparable. @JeffBezanson did an experiment where he changed Julia's Dict type to be ordered without sacrificing speed in most cases. There were a few cases where it was slower, but it was not 2x.

@kmsquire
Copy link
Member

(That implementation is now part of DataStructures.jl)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants