-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
maximum on dictionary returns max key instead of max val #14672
Comments
here's a possible function that does what I would like the default function mymax{K,V}(d::Dict{K,V})
@assert length(d) > 0
maxfound = false
maxkey = zero(K)
maxval = zero(V)
for (k,v) in d
if !maxfound
maxfound = true
maxkey = k
maxval = d[k]
elseif d[k] > maxval
maxkey = k
maxval = d[k]
end
end
Pair(maxkey,maxval)
end |
Pairs are ordered lexicographically, but reductions on |
This is a decent argument for making pairs compare their RHS first and LHS second, since the primary application is to represent the data in an associative mapping and getting the maximum key is, as you point out, simple, whereas there's no obvious way to get the key associated with the maximum value. Trying this out seems pretty decent: julia> Base.isless(p::Pair, q::Pair) =
ifelse(!isequal(p.second,q.second),
isless(p.second,q.second),
isless(p.first,q.first)
)
isless (generic function with 44 methods)
julia> d = Dict{Int,Int}(1=>2,3=>6,5=>10,7=>8)
Dict(7=>8,3=>6,5=>10,1=>2)
julia> maximum(values(d))
10
julia> maximum(keys(d))
7
julia> maximum(d)
5=>10
julia> maximum(d)[1]
5 It's kind of an unintuitive ordering though, and getting the key for the maximum value this way does have the side effect that it compares keys as well as values, which you don't necessarily care about – you just want some key that maps to the maximum value, and may not even care about the keys being comparable. Of course, in that case, you can just find all the keys equal to the maximum value. |
Indeed, it doesn't look like this ordering for pairs is really worth it. It would make more sense to special-case |
I just ran into this as well, from the findmax direction. julia> d = Dict{Int,Int}(1=>2,3=>6,5=>10,7=>8)
Dict{Int64,Int64} with 4 entries:
7 => 8
3 => 6
5 => 10
1 => 2
julia> findmax(d)
(7=>8,1) Not only does it find the largest key, which is of questionable value, but it also returns the index with respect to an arbitrary internal order, which seems completely useless. I'm in favor of specialcasing reductions on associatives, focusing on values rather than keys. |
bump. i agree it's a little weird that findmax checks keys instead of values |
I've been thinking about Dict iteration a bunch lately, and how dealing with Pairs is generally difficult. See: |
For the specific case of finding the key with the largest value, it seems |
Part of #20402 |
|
I don't really know how this works in Matlab, but I find the current situation extremely confusing and error prone:
finding the maximum of the keys is easy with
maximum(keys(d))
. Finding the key with the maximum value on the other hand currently requires writing a small function. Unless I'm missing something of course :)So, is this behaviour intentional?
The text was updated successfully, but these errors were encountered: